1. 程式人生 > >Spring框架——AOP前置、後置、環繞、異常通知

Spring框架——AOP前置、後置、環繞、異常通知

通知型別:


步驟:

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