您的位置:首页 > 编程语言 > Java开发

Spring框架——AOP前置、后置、环绕、异常通知

2016-08-05 21:21 796 查看
通知类型:



步骤:

1. 定义接口

2. 编写对象(被代理对象=目标对象)

3. 编写通知(前置通知目标方法调用前调用)

4. 在beans.xml文件配置

4.1 配置 被代理对象=目标对象

4.2 配置通知

4.3 配置代理对象 是 ProxyFactoryBean的对象实例

4.3.1 <!-- 代理接口集 -->

4.3.2 织入通知

4.3.3 配置被代理对象

本实例:

1、基本信息:

包名:com.aop

两个接口类:TestServiceInter.java;TestServiceInter2.java

测试类:Test1Service.java实现了上述两个接口

前置通知类:MyMethodBeforeAdvice.java

后置通知类:MyAfterReturningAdvice.java

环绕通知类:MyMethodInterceptor.java

异常通知类:MyThrowsAdvice.java

配置文件:beans.xml

应用操作类:App.java

2、接口类TestServiceInter.java中代码:

package com.aop;

public interface TestServiceInter {

public void sayHello();
}3、接口类TestServiceInter2.java中代码:

package com.aop;

public interface TestServiceInter2 {
public void sayBye();
}4、测试类Test1Service.java中代码:

package com.aop;

public class Test1Service implements TestServiceInter,TestServiceInter2 {

private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello() {

System.out.println("Hello!"+name);

}
public void sayBye() {
// TODO Auto-generated method stub
System.out.println("Bye!"+name);
}
}5、前置通知类MyMethodBeforeAdvice.java中代码:

package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

@Override//method:表示被调用的方法,args:给这个方法传递的参数;target:目标对象

public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("***************************");
System.out.println("前置通知,记录日志..."+method.getName());
}
}6、后置通知类MyAfterReturningAdvice.java中代码:
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class MyAfterReturningAdvice implements AfterReturningAdvice {

@Override
public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("后置通知,关闭资源...");
}
}
7、环绕通知类MyMethodInterceptor.java中的代码:

package com.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {

@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stub
System.out.println("环绕通知,调用环绕方法前...");
Object object=arg0.proceed();
System.out.println("环绕通知,调用环绕方法后...");
return null;
}
}8、异常通知类MyThrowsAdvice.java中的代码:

在代码中设置一个异常即可检验出异常通知,例如int a=9/0异常;
package com.aop;

import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;

public class MyThrowsAdvice implements ThrowsAdvice {

public void afterThrowing(Method m,Object[] os,Object target,Exception e) {

System.out.println("出异常了..."+e.getMessage());
}
}9、配置文件beans.xml中代码:
<!-- 1、配置被代理的对象,该对象实现了接口 -->
<bean id="test1Service" class="com.aop.Test1Service">
<property name="name" value="小明"/>
</bean>
<!-- 2、配置前置通知-->
<bean id ="myMethodBeforeAdvice" class="com.aop.MyMethodBeforeAdvice" />
<!-- 配置后置通知 -->
<bean id="myAfterReturningAdvice" class="com.aop.MyAfterReturningAdvice"/>
<!-- 配置环绕通知 -->
<bean id="myMethodInterceptor" class="com.aop.MyMethodInterceptor"/>
<!-- 配置异常通知 -->
<bean id="myThrowsAdvice" class="com.aop.MyThrowsAdvice"/>
<!-- 定义前置通知的切入点 -->
<bean id="myMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myMethodBeforeAdvice" />
<property name="mappedNames">
<value>sayHello</value>
</property>
</bean>
<!-- 3、配置代理对象,spring提供 -->
<bean id="proxyFactoryBean1" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 3.1、配置代理接口集-->
<property name="proxyInterfaces">
<list>
<value>com.aop.TestServiceInter</value>
<value>com.aop.TestServiceInter2</value>
</list>
</property>
<!-- 3.2、把通知织入到代理对象 -->
<property name="interceptorNames">
<list>
<!-- 相当于把前置通知和代理对象关联起来,可以把通知看成拦截器 -->
<!-- 使用自定义切入点来控制前置通知 -->
<!-- <value>myMethodBeforeAdviceFilter</value> -->
<!-- 织入前置通知 -->
<value>myMethodBeforeAdvice</value>
<!-- 织入后置通知 -->
<value>myAfterReturningAdvice</value>
<!-- 织入环绕通知 -->
<value>myMethodInterceptor</value>
<!-- 织入异常通知 -->
<value>myThrowsAdvice</value>
</list>
</property>
<!-- 3.3、配置被代理对象 ,可以指定-->
<property name="target" ref="test1Service"/>
</bean>
10、应用操作类App.java中代码:

package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext ac=new ClassPathXmlApplicationContext("com/aop/beans.xml");
TestServiceInter ts=(TestServiceInter)ac.getBean("proxyFactoryBean1");
TestServiceInter2 ts2=(TestServiceInter2)ts;//转接口
ts.sayHello();
ts2.sayBye();
}
}11、运行结果:



12、项目源码:
http://download.csdn.net/detail/tingzhiyi/9596426
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java Spring aop