1. 程式人生 > >基於代理的spring aop中,使用xml實現通知和引入

基於代理的spring aop中,使用xml實現通知和引入

ProxyFactoryBean xml配置中,實現代理工廠的類
屬性 定義
target 代理的目標物件
proxyInterfaces 代理需要實現的介面
interceptorNames 需要weaving的目標物件的bean列表,這些bean列表 org.springframework.cglib.proxy.MethodInterceptor或 org.springframework.aop.Advisor. 呼叫的先後順序按配置的先後順序.
singleton 返回的代理例項是否是單例模式的
optimize 是否強制設定cglib代理模式
proxyTargetClass 是否對類進行代理

目標物件類

package siye;
public class TargetObj
{
	public void work()
	{
		System.out.println("work");
	}
	public void throwOwn()
	{
		try
		{
			throw new RuntimeException();
		} catch (Exception e)
		{
		}
	}
}

目標物件新增特性的介面

package siye;
public interface NewFeature
{
	void eat();
}

前置通知實現類

package siye;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MethodBeforeAdviceImpl implements MethodBeforeAdvice
{
	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable
	{
		System.out.println("方法即將被呼叫.");
	}
}

返回通知實現類

package siye;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterReturningAdviceImpl implements AfterReturningAdvice
{
	@Override
	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable
	{
		System.out.println("方法已返回,返回值是," + returnValue);
	}
}

環繞通知實現類

package siye;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MethodInterceptorImpl implements MethodInterceptor
{
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable
	{
		System.out.println("環繞攔截開始....");
		Object obj = invocation.proceed();
		System.out.println("環繞攔截結束");
		return obj;
	}
}

異常通知實現類

package siye;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ThrowsAdviceImpl implements ThrowsAdvice
{
	public void afterThrowing(Method method, Object[] args,
			Object target, Exception ex) throws Throwable
	{
		System.out.println("方法丟擲異常,異常型別是," + ex.toString());
	}
}

引入實現類

package siye;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
public class IntroductionInterceptorImpl extends
		DelegatingIntroductionInterceptor implements NewFeature
{
	private static final long serialVersionUID = 1401264437483157075L;
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable
	{
		return super.invoke(mi);
	}
	@Override
	public void eat()
	{
		System.out.println("introduction__eat....");
	}
}

xml配置檔案,檔名`config.xml`

<bean id="adviceBefore" class="siye.MethodBeforeAdviceImpl" />     
<bean id="adviceAfterReturning"                                    
	class="siye.AfterReturningAdviceImpl" />                       
<bean id="adviceAround" class="siye.MethodInterceptorImpl" />      
<bean id="adviceThrows" class="siye.ThrowsAdviceImpl" />           
<bean id="introductionCustom"                                      
	class="siye.IntroductionInterceptorImpl" />                    
<bean id="target" class="siye.TargetObj" />                        
<bean id="proxyFactory"                                            
	class="org.springframework.aop.framework.ProxyFactoryBean">    
	<property name="interfaces" value="siye.NewFeature" />         
	<property name="interceptorNames">                             
		<list>                                                     
			<idref bean="adviceBefore" />                          
			<idref bean="adviceAfterReturning" />                  
			<idref bean="adviceAround" />                          
			<idref bean="adviceThrows" />                          
			<idref bean="introductionCustom" />                    
		</list>                                                    
	</property>                                                    
	<property name="target" ref="target" />                        
	<property name="proxyTargetClass" value="true" />              
</bean>                                                            

測試類

package siye;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTest
{
	public static void main(String[] args)
	{
		ClassPathXmlApplicationContext context =
				new ClassPathXmlApplicationContext(
						"classpath:/siye/config.xml");
		TargetObj targetObj =
				(TargetObj) context.getBean("proxyFactory");
		targetObj.work();
		System.out.println();
		try
		{
			targetObj.throwOwn();
		} catch (Exception e)
		{
		}
		System.out.println();
		NewFeature obj = (NewFeature) targetObj;
		obj.eat();
		context.close();
	}
}