Spring Framework では、AOP を使用しているときに奇妙な問題に直面しています。挨拶用に次の単純な Bean クラスがあります。
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");
}
}
春の設定の下:
<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() メソッドでポイントカットを使用すると、アドバイスが正しく機能しますか??