带注释的PRIVATE方法的AspectJ切入点 [英] AspectJ pointcut for annotated PRIVATE methods

查看:326
本文介绍了带注释的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?

推荐答案

由于Spring的AOP框架基于代理的性质,因此,无论是JDK代理(不适用于此方法)还是CGLIB代理(在技术上可行但不建议AOP使用),根据定义,都不会截获受保护的方法目的).结果,任何给定的切入点将仅与公共方法匹配!

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!

如果您的拦截需求包括受保护的/私有方法,甚至构造函数,请考虑使用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切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆