AOP增强详解
来源:互联网 发布:淘宝常促网 编辑:程序博客网 时间:2024/06/01 09:57
增强是AOP的核心部分之一,它包含逻辑代码和方位,增强就是一重最简单的切面。
增强的类型:
- 前置增强
- 后置增强
- 环绕增强
- 异常抛出增强
- 引介增强
package com.smart.advice; public interface Waiter { //接口 void greetTo(String name); void serveTo(String name);}
package com.smart.advice;public class NaiveWaiter implements Waiter {//接口实现类(目标类) public void greetTo(String name) { System.out.println("greet to "+name+"..."); } public void serveTo(String name){ System.out.println("serving "+name+"..."); }}
package com.smart.advice;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice; //增强类public class GreetingBeforeAdvice implements MethodBeforeAdvice {//定义的一个接口,before是唯一的方法,par1 目标类的方法,par2 目标类的入参,par3 目标类 public void before(Method method, Object[] args, Object obj) throws Throwable { String clientName = (String)args[0]; System.out.println("How are you!Mr."+clientName+"."); }}
package com.smart.advice;import org.springframework.aop.BeforeAdvice;import org.springframework.aop.framework.ProxyFactory;import static org.testng.Assert.*;import org.testng.annotations.*;public class BeforeAdviceTest { @Test public void before() { Waiter target = new NaiveWaiter(); //目标类 BeforeAdvice advice = new GreetingBeforeAdvice();//增强类 ProxyFactory pf = new ProxyFactory(); //代理 pf.setInterfaces(target.getClass().getInterfaces());//指定代理的接口 pf.setOptimize(true);//指定底层处理方式为CGlib pf.setTarget(target);//指定目标类 pf.addAdvice(advice);//指定增强 Waiter proxy = (Waiter)pf.getProxy(); //获得代理 proxy.greetTo("John"); proxy.serveTo("Tom"); }}
主要思想创建前置增强,使用代理。
ProxyFactory 是一个代理工厂,他的底层就是使用jdk动态代理或者CGlib动态代理,根据情况自动选择,也可以使用setOptimize(true)强制使用CGlib代理。
下面介绍一下spring中配置增强的方法:
<bean id="greetingBefore" class="com.smart.advice.GreetingBeforeAdvice" /> <bean id="greetingAfter" class="com.smart.advice.GreetingAfterAdvice" /> <bean id="greetingAdvice" class="com.smart.advice.GreetingBeforeAdvice" /> <bean id="greetingAround" class="com.smart.advice.GreetingInterceptor" /> <bean id="target" class="com.smart.advice.NaiveWaiter" /><bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean" p:proxyInterfaces="com.smart.advice.Waiter"//指定接口 p:target-ref="target"//指定目标类 p:interceptorNames="greetingAround【,greetingAfter】" />//指定增强,可以指定多个增强,标准格式为: // <property name = "interceptorNames"> // <list> // <idref local = "greetingBefore"/> // <idref local = "greetingAfter"/> // </list> // </property> // p:optimize = true;//指定底层实现方式
通过spring配置的增强如何使用?见下面代码:
@Test public void advice() { String configPath = "com/smart/advice/beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);//获取资源 Waiter waiter = (Waiter)ctx.getBean("waiter");//获取bean,通过spring配置,该Bean以及被加强 waiter.greetTo("John"); }
对于后置增强:其他使用方法和前置增强一样。下面我们看看如何定义后置增强,
package com.smart.advice;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!"); }}
环绕增强的定义方法:
package com.smart.advice;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; }}
异常增强的定义方法:
package com.smart.advice;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("成功回滚事务。"); }}
引介增强:它不是在目标方法周围织入增强,而是为目标类创建新的方法和属性,连接点就是类级别的。
它能干嘛呢?比如我们织入增强,需要手动控制增强的开启与关闭,这时就要使用引介增强了。
package com.smart.introduce;import org.aopalliance.intercept.MethodInvocation;//引介增强提供的实现类,所以这里不是实现,而是继承import org.springframework.aop.support.DelegatingIntroductionInterceptor;public class ControllablePerformaceMonitor extends DelegatingIntroductionInterceptor implements Monitorable, Testable { //线程局部变量,支持范型 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; } public void test() { // TODO Auto-generated method stub System.out.println("dd"); }}
<bean id="pmonitor" class="com.smart.introduce.ControllablePerformaceMonitor" /> <bean id="forumServiceTarget" class="com.smart.introduce.ForumService" /> <bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean" //引介增强实现的接口 p:interfaces="com.smart.introduce.Monitorable" //目标类 p:target-ref="forumServiceTarget" //引介增强 p:interceptorNames="pmonitor" //引介增强必须使用CGlib动态代理,这里必须设置为true p:proxyTargetClass="true" />
@Test public void introduce(){ String configPath = "com/smart/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); }
增强都是需要实现特定的接口,和方法、
然后通过代理,设置代理的接口,目标类,增强,底层代理方式。从而实现增强的织入,可是这种织入,是对目标类的所有方法而言的。如果我们只想对目标类的某一方法进行处理,我们该如何处理呢?这时候就要使用切面了。
增强+切点=切面。增强就是最简单的一种切面。
阅读全文
0 0
- AOP增强详解
- spring AOP详解之--前置增强 (MethodBeforeAdvice)
- Spring AOP 增强类型
- Spring AOP--引入增强
- Spring AOP增强类型
- Spring Aop增强
- spring aop 增强
- Spring AOP 增强类型
- aop 经典增强
- AOP-方法顾问增强
- 字节码增强和AOP
- 七.Spring AOP 之增强
- Spring AOP增强小例子
- Spring AOP增强小例子
- Spring AOP之增强介绍
- Spring AOP之增强(aspectj)
- Spring学习,AOP增强类型
- AOP详解
- ApiDemos学习知识点之Sensors
- Zookeeper watcher机制 所涉及的类
- Java bean概念
- Demo013定向输出实现日志
- 新手如何掌握制作和提交网站地图?
- AOP增强详解
- 前端 排序算法总结
- 线程基础知识
- AngularJs路由
- GitHub开源:升讯威微信营销系统(第三方微信平台)完整源代码
- jdbcType与javaType的对应关系
- 浙江大学数据结构第一周编程作业【Java】
- bzoj1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
- swap函数的实现方式