简体   繁体   English

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

[英]Spring - AspectJ pointcut for constructor object with annotation

I'm developing a java (JDK1.6) application with Spring framework(4.0.5) and AspectJ for AOP Logging. 我正在使用Spring框架(4.0.5)和AspectJ进行AOP Logging开发一个java(JDK1.6)应用程序。

My Aspect classes work fine but I can't create a pointcut for constructor object. 我的Aspect类工作正常,但我无法为构造函数对象创建切入点。

This is my object: 这是我的目标:

@Controller
public class ApplicationController {
    public ApplicationController(String myString, MyObject myObject) {
        ...
    }
    ...
    ..
    .
}

This is my Aspect class: 这是我的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");
    }
}

How can I create a pointcut for my constructor object? 如何为构造函数对象创建切入点?


Thanks 谢谢

Sotirios Delimanolis is right insofar as Spring AOP does not support constructor interception, you do need full AspectJ for it. Sotirios Delimanolis是正确的,因为Spring AOP不支持构造函数拦截,你需要完整的AspectJ。 The Spring manual, chapter 9.8 Using AspectJ with Spring applications , describes how to use it with LTW (load-time weaving). Spring手册,第9.8将AspectJ与Spring应用程序一起使用,描述了如何将它与LTW(加载时编织)一起使用。

Furthermore, there is a problem with your pointcut 此外,您的切入点存在问题

@Before("execution(* *.new(..))")

Constructors do not have return types like methods in AspectJ syntax, so you need to remove the leading * : 构造函数没有像AspectJ语法中的方法那样的返回类型,因此您需要删除前导*

@Before("execution(*.new(..))")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM