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()方法上使用切入點?