Search code examples
springaopspring-aspects

Must set property 'expression' before attempting to match


When I was trying to solve this SO question...

I faced

Must set property 'expression' before attempting to match

Solution

  • Problem was, that I had no value for pointcut in @AfterThrowing annotation:

    wrong

    @AfterThrowing(throwing = "ex")
    public void intercept(DataAccessException ex) throws Exception {
        //throw DatabaseException
        System.out.println("DAE");
        throw new IllegalArgumentException("DAE");
    }
    
    @AfterThrowing(throwing = "ex")
    public void intercept(RuntimeException ex) throws Exception {
        //throw ServiceException
        System.out.println("RE - " + ex.getClass());
        throw new IllegalArgumentException("RE");
    }
    

    correct

    @AfterThrowing(pointcut = "execution(public * *(..))", throwing = "ex")
    public void intercept(DataAccessException ex) throws Exception {
        //throw DatabaseException
        System.out.println("DAE");
        throw new IllegalArgumentException("DAE");
    }
    
    @AfterThrowing(pointcut = "execution(public * *(..))", throwing = "ex")
    public void intercept(RuntimeException ex) throws Exception {
        //throw ServiceException
        System.out.println("RE - " + ex.getClass());
        throw new IllegalArgumentException("RE");
    }
    

    Similar question I found was AspectJExpressionPointcut uses wrong classLoader, but I was not dealing with class loader problem...