Spring - 带注释的构造函数对象的AspectJ切入点

Pap*_*urf 8 java aop spring aspectj spring-aop

我正在使用Spring框架(4.0.5)和AspectJ进行AOP Logging开发一个java(JDK1.6)应用程序.

我的Aspect类工作正常,但我无法为构造函数对象创建切入点.

这是我的目标:

@Controller
public class ApplicationController {
    public ApplicationController(String myString, MyObject myObject) {
        ...
    }
    ...
    ..
    .
}
Run Code Online (Sandbox Code Playgroud)

这是我的Aspect类:

@Aspect
@Component
public class CommonLogAspect implements ILogAspect {
    Logger log = Logger.getLogger(CommonLogAspect.class);

    // @Before("execution(my.package.Class.new(..)))
    @Before("execution(* *.new(..))")
    public void constructorAnnotatedWithInject() {
        log.info("CONSTRUCTOR");
    }
}
Run Code Online (Sandbox Code Playgroud)

如何为构造函数对象创建切入点?


谢谢

kri*_*aex 12

Sotirios Delimanolis是正确的,因为Spring AOP不支持构造函数拦截,你需要完整的AspectJ.春季手册,章节9.8中使用AspectJ的Spring应用程序,介绍了如何使用LTW(加载时织)使用它.

此外,您的切入点存在问题

@Before("execution(* *.new(..))")
Run Code Online (Sandbox Code Playgroud)

构造函数没有像AspectJ语法中的方法那样的返回类型,因此您需要删除前导*:

@Before("execution(*.new(..))")
Run Code Online (Sandbox Code Playgroud)