[英]Java - Spring AOP Pointcut not working
Can someone pinpoint what am I doing wrong? 有人可以指出我在做什么错吗? How can I make my Aspect run? 如何使我的Aspect运行?
I have written this code following some examples: 我已经通过以下示例编写了此代码:
@Aspect
public class MethodLogger {
private Logger log = Logger.getLogger(getClass().getName());
@Pointcut("execution(* setHeight(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " called...");
}
}
My simple test class: 我的简单测试类:
public class ChairImpl implements Chair {
int height;
public ChairImpl(){}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
My Spring xml config: 我的Spring xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-destroy-method="destroy"
default-init-method="afterPropertiesSet"
default-autowire="byName"
>
<aop:aspectj-autoproxy>
<aop:include name="MethodLogger"/>
</aop:aspectj-autoproxy>
<bean id="logger1" class="lab.MethodLogger"/>
<bean id="chair1"
class="lab.test.ChairImpl"
>
<property name="height" value="10"/>
</bean>
</beans>
And my main method: 而我的主要方法是:
public class Main {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spec.xml");
((AbstractApplicationContext) context).close();
}
}
So before running my project, Eclipse gives me this error(it marks red void
word for log
method): 因此,在运行我的项目之前,Eclipse给了我这个错误(它将log
方法标记为红色void
):
Pointcuts without an if() expression should have an empty method body
When I run, my program runs without errors, because it looks like log
method never ran. 当我运行时,我的程序运行没有错误,因为它看起来好像从未运行过log
方法。 So how could I fix it so it would run and output log? 那么我该如何解决它以便运行并输出日志? I tried simply to print test text
from that method, but it never does, so it means it never runs. 我只是尝试从该方法打印test text
,但是它从未执行过,因此这意味着它永远不会运行。 What am I missing here? 我在这里想念什么?
In documentation it only writes vague examples like: 在文档中,它只写模糊的示例,例如:
@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature
But I find it hard to understand how to actually use it to see results. 但是我发现很难理解如何实际使用它来查看结果。
You can try an anonymous pointcut this way: 您可以通过以下方式尝试匿名切入点:
@Before("execution(* setHeight(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " called...");
}
or give your pointcut a name and use it this way: 或给切入点起一个名字并以这种方式使用它:
@Pointcut("execution(* setHeight(..))")
public void setters() {}
@Before("setters()")
public void log(JoinPoint point) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.