浅谈spring——增强接口(六)
来源:互联网 发布:与制工作室长板淘宝 编辑:程序博客网 时间:2024/06/07 09:31
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!");
- }
- }
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;
- }
- }
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("成功回滚事务。");
- }
- }
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" />
- lt;/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"
可以直观理解为下图:
0 0
- 浅谈spring——增强接口(六)
- 浅谈spring——增强接口(六)
- Spring源码阅读(六)—AOP获取增强
- spring切面:接口:环绕增强
- spring切面:接口:异常增强
- Java核心技术(六) —— 接口
- j2se学习要点记录(六)collection(集合)接口、增强的for循环、collections类
- spring切面:接口:前后置增强
- 浅谈jQuery源码(六)——$.map
- 浅谈spring——引言(一)
- 浅谈spring——切面(七)
- 深度增强学习David Silver(六)——Value Function Approximation
- 浅谈SVM(六)
- 浅谈spring——spring MVC(十一)
- 浅谈spring——Spring资源加载(十二)
- Spring学习——(六)Spring与Hibernate整合
- Java基础(六)——接口和抽象类
- Java-Collection源码分析(六)——Map接口
- HDU 2084 数塔
- 【算法编程】循环右移一个数组
- 动态链接库dll的使用方法——动态加载dll
- hadoop 1.X资源管理机制缺陷分析和解决方案
- 文章点击数加1
- 浅谈spring——增强接口(六)
- 四.Python流程控制の(二)逻辑符
- HDU 1176 免费馅饼
- SSH框架+Oracle整合之问题2
- 数据结构与算法分析学习笔记--第四章(二叉树:创建、递归遍历、非递归遍历、根据数据删除结点等)
- 三、cocos是如何将c++类导出到lua的?
- 母函数入门+模板
- 设计模式—原型、模板方法模式
- LeetCode First Missing Positive