`
阿尔萨斯
  • 浏览: 4177079 次
社区版块
存档分类
最新评论

浅谈spring——增强接口(六)

 
阅读更多

AOP支持5种类型的增强。


增强接口关系图:


1)前置增强:BeforeAdvice,表示在目标方法执行前实施增强。

2)后置增强:AfterReturningAdvice,表示在目标方法执行后实施增强。

3)环绕增强:MethodInterceptor,表示在目标方法执行前后实施增强

4)异常增强:ThrowsAdvice,表示在目标方法抛出异常后实施增强

5)引介增强:IntroductionInterceptor,表示在目标类中添加一些新的属性和方法


xml配置:

	<bean id="greetingBefore" class="com.baobaotao.advice.GreetingBeforeAdvice" />
	<bean id="target" class="com.baobaotao.advice.NaiveWaiter" />
	<bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
		p:proxyInterfaces="com.baobaotao.advice.Waiter"   ##代理接口
		p:target-ref="target"  ##指定对哪个bean增强
		p:interceptorNames="greetingBefore"/>    ##指定的增强

代码实例

1)前置增强:

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class GreetingBeforeAdvice implements MethodBeforeAdvice {
	public void before(Method method, Object[] args, Object obj) throws Throwable {
		String clientName = (String)args[0];
		System.out.println("How are you!Mr."+clientName+".");
	}
}

method:目标类的方法

args:目标类方法的入参

obj:目标类实例


2)后置增强:

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

public class GreetingAfterAdvice implements AfterReturningAdvice {

	public void afterReturning(Object returnObj, Method method, Object[] args,
			Object obj) throws Throwable {
		System.out.println("Please enjoy yourself!");
	}
}
returnObj:目标实例方法返回的结果

method:目标类方法

args:目标实例方法的入参

obj:目标类实例


3)环绕增强

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class GreetingInterceptor implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object[] args = invocation.getArguments();//获取入参
		String clientName = (String)args[0];
		System.out.println("How are you!Mr."+clientName+".");
		
		Object obj = invocation.proceed();//反射机制调用目标方法
		
		System.out.println("Please enjoy yourself!");
		
		return obj;
	}
}
MethodInvocation不但封装了目标方法及其入参数组,还封装了目标方法所在的实例对象


4)抛出异常的增强

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class TransactionManager implements ThrowsAdvice {
	public void afterThrowing(Method method, Object[] args, Object target,
			Exception ex) throws Throwable {
		System.out.println("-----------");
		System.out.println("method:" + method.getName());
		System.out.println("抛出异常:" + ex.getMessage());
		System.out.println("成功回滚事务。");
	}
}
ThrowAdvice异常抛出增强接口没有定义任何方法,它是一个标识接口,在运行时由spring使用反射机制自行判断。

method:目标类方法

args:目标实例方法的入参

target:目标类实例

ex:要捕获的异常

其中前三个参数可以缺省。afterThrowing方法,可以定义多个,当目标方法抛出异常时,spring会自动找出最匹配的增强方法。


5)引介增强:比较特殊,不是在目标方法周围织入增强,而是为目标类增加新的方法和属性,属于类级别而非方法级别。

接口

public interface Monitorable {
   void setMonitorActive(boolean active);
}


增强代码

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
public class ControllablePerformaceMonitor extends
			DelegatingIntroductionInterceptor implements Monitorable{
			
	private ThreadLocal<Boolean> MonitorStatusMap = new ThreadLocal<Boolean>();
	public void setMonitorActive(boolean active) {
		MonitorStatusMap.set(active);
	}
	public Object invoke(MethodInvocation mi) throws Throwable {
		Object obj = null;
		if (MonitorStatusMap.get() != null && MonitorStatusMap.get()) {
			PerformanceMonitor.begin(mi.getClass().getName() + "."
					+ mi.getMethod().getName());
			obj = super.invoke(mi);
			PerformanceMonitor.end();
		} else {
			obj = super.invoke(mi);
		}
		return obj;
	}

}

xml配置

	<bean id="pmonitor" class="com.baobaotao.introduce.ControllablePerformaceMonitor" />
	<bean id="forumServiceTarget" class="com.baobaotao.introduce.ForumService" />
	<bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean"
		p:interfaces="com.baobaotao.introduce.Monitorable" 
		p:target-ref="forumServiceTarget"
		p:interceptorNames="pmonitor" 
		p:proxyTargetClass="true" />
</beans>

调用代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestIntroduce {
	public static void main(String[] args) {
		String configPath = "com/baobaotao/introduce/beans.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
        ForumService forumService = (ForumService)ctx.getBean("forumService");
        forumService.removeForum(10);
        forumService.removeTopic(1022);
		
        Monitorable moniterable = (Monitorable)forumService;   //监控开启
        moniterable.setMonitorActive(true);
        forumService.removeForum(10);
        forumService.removeTopic(1022); 
	}
}

引介增强与之前的四类增强有较大区别,首先需要指定引介增强所实现的接口Monitorable;其次只能通过为目标类创建子类的方式生成代理,必须将p:proxyTargetClass设置成"true"

可以直观理解为下图:





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics