在Spring Roo中自定义save()方法,如何访问Bean属性?

时间:2014-10-25 16:54:08

标签: aspectj spring-roo

如何在下面的after()方法中访问MyEntity Bean的属性? 例如,如果MyEntity有一个名称属性我想要访问它,我试过只是调用名称,但是Spring STS表明它是未知的,然后我尝试了MyEntity.name但是又一次未知......谢谢任何帮助

package com.malsolo.aspects;

import org.apache.log4j.Logger;
import com.malsolo.myproject.domain.MyEntity;

aspect MyEntityAspect {

    private final Logger logger = Logger.getLogger(MyEntityAspect.class);

    pointcut persistEntity() : execution(* MyEntity.persist(..));

    public Logger getLogger() {
        return logger;
    }

    after() : persistEntity() {
        logger().info("Entity persisted "+thisJoinPoint);
    }

}

2 个答案:

答案 0 :(得分:0)

可能您的财产是私人或受保护的。在这种情况下,您需要特权方面:

privileged aspect MyEntityAspect {
    // ...
}

答案 1 :(得分:0)

为了能够访问切入点所选择的特定连接点的执行上下文,您需要显式地公开它。一种方法是使用"这个"代号:

pointcut persistEntity(Object obj) : execution(* MyEntity.persist(..)) && this(obj);

after(Object obj) : persistEntity(obj) {
    // now "obj" is bound to the MyEntity instance persist() has been called on
    logger().info("Entity persisted "+thisJoinPoint);
}

你也可以使用" thisJoinPoint.getThis()"来自after()建议的方法。但请注意,此方法使用反射,因此较慢:

after() : persistEntity() {
    Object obj = thisJoinPoint.getThis();  // now "obj" is bound to the MyEntity instance
                                           // persist() has been called on
    logger().info("Entity persisted "+thisJoinPoint);
}