Java-Spring AOP切入点不工作
有人能指出我做错了什么吗?如何使我的方面运行 我编写了以下示例代码:Java-Spring AOP切入点不工作,java,spring,aspectj,pointcut,Java,Spring,Aspectj,Pointcut,有人能指出我做错了什么吗?如何使我的方面运行 我编写了以下示例代码: @Aspect public class MethodLogger { private Logger log = Logger.getLogger(getClass().getName()); @Pointcut("execution(* setHeight(..))") public void log(JoinPoint point) { log.info(point.getSig
@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...");
}
}
我的简单测试类:
public class ChairImpl implements Chair {
int height;
public ChairImpl(){}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
我的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>
所以在运行我的项目之前,Eclipse给了我这个错误(它将log
方法标记为红色void
word):
当我运行时,我的程序运行没有错误,因为它看起来像log
方法从未运行过。那么,我如何修复它,使其运行并输出日志呢?我试图简单地从该方法打印测试文本
,但它从来没有打印过,所以它意味着它永远不会运行。我错过了什么
在文档中,它只写了模糊的示例,如:
@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature
但我发现很难理解如何实际使用它来查看结果。您可以通过以下方式尝试匿名切入点:
@Before("execution(* setHeight(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " called...");
}
@Pointcut("execution(* setHeight(..))")
public void setters() {}
@Before("setters()")
public void log(JoinPoint point) {
...
}
或者给你的切入点起一个名字,然后这样使用:
@Before("execution(* setHeight(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " called...");
}
@Pointcut("execution(* setHeight(..))")
public void setters() {}
@Before("setters()")
public void log(JoinPoint point) {
...
}
你只有部分方面。你有地点,但错过了时间(周围、之前等)。在切入点(应该始终在emtpy方法上)旁边,还需要一个表示建议的方法。即,在之前用
@注释的方法。