開發環境:Eclipse
一
<?xml version=
<aop/>
package test;
publicclass HelloAOP {
/**
*@paramargs
*/
publicstaticvoid main(String[] args) {
// TODO自動生成方法存根
HelloAOP hello= new HelloAOP();
hello
}
publicvoid sayHello(){
System
}
}
package test;
import org
import org
publicclass HelloAOPInterceptor implements Interceptor {
public String getName() {
// TODO自動生成方法存根
return
}
public Object invoke(Invocation arg
// TODO自動生成方法存根
System
return arg
}
}
<?xml version=
<aop>
<bind pointcut=
<interceptor class=
</bind>
</aop>
Hello
Hello
即攔截器在sayHello方法調用前被調用
二
package test;
import org
publicclass HelloAOPAdvice {
public Object trace(Invocation invocation) throws Throwable{
System
try {
return invocation
}finally {
// TODO: handle exception
System
}
}
}
<?xml version=
<aop>
<aspect class=
<bind pointcut=
<interceptor class=
<advice aspect=
</bind>
</aop>
Hello
進入程序運行點
Hello
離開程序運行點
從此處可看到Advice在Interceptor之後介入
三
package test;
publicclass HelloAOP {
/**
*@paramargs
*/
private String message=
publicstaticvoid main(String[] args) {
// TODO自動生成方法存根
HelloAOP hello=new HelloAOP();
hello
System
hello
System
}
publicvoid sayHello(){
System
}
public String getMessage() {
returnmessage;
}
publicvoid setMessage(String message) {
ssage = message;
}
}
<?xml version=
<aop>
<aspect class=
<bind pointcut=
<interceptor class=
<advice aspect=
</bind>
<bind pointcut=
<interceptor class=
</bind>
</aop>
Hello
Hello
進入程序運行點
Hello
離開程序運行點
Hello
Welcome
Hello
Hello
Benson
當域message被訪問時(調用get或set時)
<?xml version=
<aop>
<aspect class=
<bind pointcut=
<interceptor class=
<advice aspect=
</bind>
<bind pointcut=
<interceptor class=
<interceptor class=
<advice aspect=
</bind>
</aop>
Hello
Hello
進入程序運行點
離開程序運行點
Hello
進入程序運行點
Hello
離開程序運行點
Hello
Hello
進入程序運行點
離開程序運行點
Welcome
Hello
Hello
進入程序運行點
離開程序運行點
Hello
Hello
進入程序運行點
離開程序運行點
Benson
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27198.html