带注释的PRIVATE方法的AspectJ切入点

带注释的PRIVATE方法的AspectJ切入点

本文介绍了带注释的PRIVATE方法的AspectJ切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为带有特定注释的私有方法创建切入点.但是,当注释在如下所示的私有方法上时,不会触发我的方面.

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) {
         ...
}

服务界面

public interface UserService {

    UserDto createUser(UserDto userDto);
}

服务实施

    public class UserServiceImpl implements UserService {

       public UserDto createUser(UserDto userDto) {

             validateUser(userDto);

             userDao.create(userDto);
       }

       @ValidatorMethod
       private validateUser(UserDto userDto) {

            // code here
       }

但是,如果我将注释移到公共接口方法实现createUser,则会触发我的方面.我应该如何定义切入点或配置方面以使原始用例正常工作?

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?

推荐答案

8.使用Spring进行面向方面的编程

如果您的拦截需求包括受保护的/私有方法,甚至构造函数,请考虑使用Spring驱动的本机AspectJ编织,而不是Spring的基于代理的AOP框架.这构成了具有不同特性的AOP使用模式的不同,因此请确保在做出决定之前先熟悉编织.

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.

这篇关于带注释的PRIVATE方法的AspectJ切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:42