0
votes

I am using Spring's AspectJ with CGLIB proxying. I have an aspect defined as below, where I want it to advise public methods on concrete classes that are annotated with the annotation 'ValidatorMethod':

@Aspect
public class ServiceValidatorAspect {
    @Pointcut("within(@com.example.ValidatorMethod *)")
    public void methodsAnnotatedWithValidated() {
}

@AfterReturning(
            pointcut = "methodsAnnotatedWithValidated()",
            returning = "result")
    public void throwExceptionIfErrorExists(JoinPoint joinPoint, Object result) {
         ...
}

Example Service Interface

public interface UserService {

    UserDto createUser(UserDto userDto);
}

Example Service Implementation

    public class UserServiceImpl implements UserService {

       public UserDto createUser(UserDto userDto) {

             validateUser(userDto);

             userDao.create(userDto);
       }

       @ValidatorMethod
       public void validateUser(UserDto userDto) {

            // code here
       }
}

AOP spring config:

<aop:aspectj-autoproxy proxy-target-class="true"/>

As I understand it, setting proxy-target-class to "true" will result in public methods in concrete classes being proxied, not just interface methods. However my aspect is not triggered. Any ideas as to what is wrong? I know my UserServiceImpl class is being proxied correctly by CGLIB as I can verify that in the callstack in the debugger.

1
I suggest you switch from Spring AOP to AspectJ. No proxies, full AOP power. ;-) - kriegaex

1 Answers

0
votes

Seems like the problem is this (taken from here):

Aspects do not apply to intra–operation calls. Meaning that there is no way for the proxy to intercept the calling of a method originated from another method of the same “proxy–ed” bean