0

我正在使用带有 CGLIB 代理的 Spring 的 AspectJ。我有一个定义如下的方面,我希望它在使用注释“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) {
         ...
}

示例服务接口

public interface UserService {

    UserDto createUser(UserDto userDto);
}

示例服务实现

    public class UserServiceImpl implements UserService {

       public UserDto createUser(UserDto userDto) {

             validateUser(userDto);

             userDao.create(userDto);
       }

       @ValidatorMethod
       public void validateUser(UserDto userDto) {

            // code here
       }
}

AOP 弹簧配置:

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

据我了解,设置proxy-target-class为“true”将导致具体类中的公共方法被代理,而不仅仅是接口方法。但是我的方面没有被触发。关于什么是错的任何想法?我知道我的 UserServiceImpl 类被 CGLIB 正确代理,因为我可以在调试器的调用堆栈中验证这一点。

4

1 回答 1

0

似乎问题是这样的(取自这里):

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

于 2013-03-01T02:47:43.643 回答