Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ pointcut for annotated PRIVATE methods

I want to create a Pointcut for private methods that are annotated with a specific annotation. However my aspect is not triggered when the annotation is on a private method like below.

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

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

Service Interface

public interface UserService {

    UserDto createUser(UserDto userDto);
}

Service Implementation

    public class UserServiceImpl implements UserService {

       public UserDto createUser(UserDto userDto) {

             validateUser(userDto);

             userDao.create(userDto);
       }

       @ValidatorMethod
       private validateUser(UserDto userDto) {

            // code here
       }

However if I move the annotation to a public interface method implementation createUser, my aspect is triggered. How should I define my pointcut or configure my aspect to get my original use case working?

like image 260
citress Avatar asked Feb 26 '13 16:02

citress


People also ask

What is AspectJ pointcut?

AspectJ provides two primitive pointcut designators designed to capture method call and execution join points.

What is pointcut annotation?

Pointcut: Pointcut is expressions that are matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points and Spring framework uses the AspectJ pointcut expression language.

What is AspectJ used for?

AspectJ implements both concerns and the weaving of crosscutting concerns using extensions of Java programming language.


1 Answers

8. Aspect Oriented Programming with Spring

Due to the proxy-based nature of Spring's AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn't applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring's proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

like image 54
Alexey Makarenko Avatar answered Sep 21 '22 13:09

Alexey Makarenko