一,AOP的拦截器应用
1,新建AOP工程,在新建向导中选择JBossAOP下的JBossAOP Project,输入项目名为:TestAOP,Java Version为Java 1.5,结束向导.
2,系统自动生成AOP配置文件jboss-aop.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aop/>
3,新建Java类,名为Test,勾选main方法,代码如下:
package test;
publicclass HelloAOP {
/**
*@paramargs
*/
publicstaticvoid main(String[] args) {
// TODO自动生成方法存根
HelloAOP hello= new HelloAOP();
hello.sayHello();
}
publicvoid sayHello(){
System.out.println("Hello");
}
}
4,新建拦截器.名为HelloAOPInterceptor.java,并实现Interceptor接口,代码如下:
package test;
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
publicclass HelloAOPInterceptor implements Interceptor {
public String getName() {
// TODO自动生成方法存根
return"test.HelloAOPInterceptor";
}
public Object invoke(Invocation arg0) throws Throwable {
// TODO自动生成方法存根
System.out.println("Hello,AOP!");
return arg0.invokeNext();
}
}
5,将拦截器和要拦截的类用设置文件联系起来,在”包资源管理”视图下,右击类HelloAOP的sayHello方法,在弹出菜单中选择JBoss AOP->Apply Interceptor(s)…,在Select an Interceptor..面板选择test.HelloAOPInterceptor拦截器类,系统自动更新jboss-aop.xml文件,代码为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aop>
<bind pointcut="execution(public void test.HelloAOP->sayHello())">
<interceptor class="test.HelloAOPInterceptor"/>
</bind>
</aop>
6,右击HelloAOP类,在弹出菜单中选择”运行方式”->”运行…”,在对话框中选择”JBoss AOP Application”,新建配置项.单击”运行”按钮,控制台出现如下信息:
Hello,AOP!
Hello
即拦截器在sayHello方法调用前被调用.
二,AOP的Advice应用
1,新建Java类,名为:HelloAOPAspect.java,代码如下:
package test;
import org.jboss.aop.joinpoint.Invocation;
publicclass HelloAOPAdvice {
public Object trace(Invocation invocation) throws Throwable{
System.out.println("进入程序运行点");
try {
return invocation.invokeNext();
}finally {
// TODO: handle exception
System.out.println("离开程序运行点");
}
}
}
2,与HelloAOP文件联系起来,在”包资源管理”视图下,右击类HelloAOP的sayHello方法,在弹出菜单中选择JBoss AOP->Apply Advice…,在Apply Advice…面板勾选Object trace(Invocation invocation),系统自动更新jboss-aop.xml文件,代码为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aop>
<aspect class="test.HelloAOPAdvice" scope="PER_VM"/>
<bind pointcut="execution(public void test.HelloAOP->sayHello())">
<interceptor class="test.HelloAOPInterceptor"/>
<advice aspect="test.HelloAOPAdvice" name="trace"/>
</bind>
</aop>
3,利用刚新建的运行配置运行HelloAOP.java文件,控制台出现如下信息:
Hello,AOP!
进入程序运行点
Hello
离开程序运行点
从此处可看到Advice在Interceptor之后介入,且方法sayHello()调用前后都起作用.
三,AOP类中的域应用
1,修改HelloAOP代码,添加私有变量message,利用源代码菜单,实现其Getter/Setter方法,修改main方法,完整代码如下:
package test;
publicclass HelloAOP {
/**
*@paramargs
*/
private String message="Welcome";
publicstaticvoid main(String[] args) {
// TODO自动生成方法存根
HelloAOP hello=new HelloAOP();
hello.sayHello();
System.out.println(hello.getMessage());
hello.setMessage("Benson");
System.out.println(hello.getMessage());
}
publicvoid sayHello(){
System.out.println("Hello");
}
public String getMessage() {
returnmessage;
}
publicvoid setMessage(String message) {
this.message = message;
}
}
2, 在”包资源管理”视图下,右击类HelloAOP的域message,在弹出菜单中选择JBoss AOP->Apply Interceptor(s)…,在Select an Interceptor..面板选择test.HelloAOPInterceptor拦截器类,系统自动更新jboss-aop.xml文件,文件内容为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aop>
<aspect class="test.HelloAOPAdvice" scope="PER_VM"/>
<bind pointcut="execution(public void test.HelloAOP->sayHello())">
<interceptor class="test.HelloAOPInterceptor"/>
<advice aspect="test.HelloAOPAdvice" name="trace"/>
</bind>
<bind pointcut="field(private java.lang.String test.HelloAOP->message)">
<interceptor class="test.HelloAOPInterceptor"/>
</bind>
</aop>
3, 利用刚新建的运行配置运行HelloAOP.java文件,控制台出现如下信息:
Hello,AOP!
Hello,AOP!
进入程序运行点
Hello
离开程序运行点
Hello,AOP!
Welcome
Hello,AOP!
Hello,AOP!
Benson
当域message被访问时(调用get或set时),都会调用拦截器.可以看得出拦截器在对象初始化时,对域message也起作用.
4,利用相似的方法,可以给域message设置advice.在”包资源管理”视图下,右击类HelloAOP的域message,在弹出菜单中选择JBoss AOP->Apply Advice…,在Apply Advice…面板勾选Object trace(Invocation invocation),系统自动更新jboss-aop.xml文件,代码为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aop>
<aspect class="test.HelloAOPAdvice" scope="PER_VM"/>
<bind pointcut="execution(public void test.HelloAOP->sayHello())">
<interceptor class="test.HelloAOPInterceptor"/>
<advice aspect="test.HelloAOPAdvice" name="trace"/>
</bind>
<bind pointcut="field(private java.lang.String test.HelloAOP->message)">
<interceptor class="test.HelloAOPInterceptor"/>
<interceptor class="test.HelloAOPInterceptor"/>
<advice aspect="test.HelloAOPAdvice" name="trace"/>
</bind>
</aop>
5,利用刚新建的运行配置运行HelloAOP.java文件,控制台出现如下信息:
Hello,AOP!
Hello,AOP!
进入程序运行点
离开程序运行点
Hello,AOP!
进入程序运行点
Hello
离开程序运行点
Hello,AOP!
Hello,AOP!
进入程序运行点
离开程序运行点
Welcome
Hello,AOP!
Hello,AOP!
进入程序运行点
离开程序运行点
Hello,AOP!
Hello,AOP!
进入程序运行点
离开程序运行点
Benson