1
在Spring Framework中,我在使用AOP时遇到了一个奇怪的问题。 我有一个问候下面这个简单的bean类:Spring AOP之后,建议在getter函数无法正常工作
public class HelloBean {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void displayGreeting() {
System.out.println("Hello");
}
}
下面的Spring配置:
<beans>
<bean id="hello" class="com.att.spring.main.HelloBean"/>
<bean id="serviceCheck" class="com.att.spring.main.ServiceCheck" />
<aop:config>
<aop:aspect ref="serviceCheck">
<aop:pointcut id="greet"
expression="execution(* *.getMessage(..))" />
<aop:before pointcut-ref="greet"
method="preRunMessage" />
<aop:after pointcut-ref="greet"
method="postRunMessage" />
</aop:aspect>
</aop:config>
</beans>
AOP通知方法:
public class ServiceCheck {
public void preRunMessage() {
System.out.println("Runs before the greeting");
}
public void postRunMessage() {
System.out.println("Runs after the greeting");
}
}
测试类:
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-beans.xml");
HelloBean hello = (HelloBean) context.getBean("hello");
hello.setMessage("Hello World");
System.out.println(hello.getMessage());
}
}
输出:
Runs before the greeting
Runs after the greeting
Hello World
问:
为什么都建议(前,后),当我使用getter为切入点得到打印。建议工作正确,当我在displayGreeting()方法上使用切入点?