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"
可以直观理解为下图:
分享到:
相关推荐
所有Spring——jar详细介绍 所有Spring——jar详细介绍 所有Spring——jar详细介绍
关于Spring方面的常见面试题
spring——第十四节素材
浅谈 Spring 原理 透析,ioc aop
spring——mvc下的简单程序,适合新手使用
NULL 博文链接:https://teddywang.iteye.com/blog/357901
SpringCloud——分布式配置中心(Spring Cloud Config)之高可用的分布式配置中心
浅谈Spring+Hibernate整合(共16页).doc
NULL 博文链接:https://shiyongno1.iteye.com/blog/2181031
SpringCloud——消息总线(Bus)之Spring Cloud Bus将分布式系统的节点与轻量级消息代理链接。
SpringCloud——分布式跟踪(Sleuth)之Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案。
SpringCloud——断路器(Hystrix)之Ribbon使用断路器和Feign使用断路器
Spring boot——@DeclareParents例子...
SpringCloud——服务注册(consul)之Consul通过HTTP API和DNS提供服务发现服务。
SpringCloud——路由器和过滤器(Zuul)之微服务网关的实现
本文来自程序猿,本文主要介绍了SpringCloud微服务下服务接口调试及管理,什么样方式可以让微服务的接口管理变得更加容易些,希望对您的学习有所帮助。我们知道在微服务架构下,软件系统会被拆分成很多个独立运行的...
{"code":200,"data":[{"age":12,"name":"12","sex":"男","username":"张三","userpassword":"123"},{"age":22,"name":"12","sex":"女","username":"李白","userpassword":"4576"}]}
IOC与DI的理解及使用 控制反转IOC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IOC的一种方法 。... 在Spring中实现控制反转的是IOC容器 ,其 实现方法是依赖注入 (Dependency Injection,DI)
上文我们利用Spring rmi实现了Spring的远程访问(Spring 实现远程访问详解——rmi),本文主要讲解利用HttpInvoke实现远程访问。 Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和...