AOP 增强方法
Spring AOP 提供了 5 种类型的通知,它们分别是 Before Advice(前置通知)、After Returning Advice(后置通知)、Interception Around Advice(周围通知)、Throws Advice(异常通知)和 Introduction Advice(引介通知)。

下面分别进行介绍。
- 前置增强 (org.springframework.aop.BeforeAdvice) 表示在目标方法执行前来实施增强
- 后置增强 (org.springframework.aop.AfterReturningAdvice) 表示在目标方法执行后来实施增强
- 环绕增强 (org.aopalliance.intercept.MethodInterceptor) 表示在目标方法执行前后同时实施增强
- 异常抛出增强 (org.springframework.aop.ThrowsAdvice) 表示在目标方法抛出异常后来实施增强
- 引介增强 (org.springframework.aop.introductioninterceptor) 表示在目标类中添加一些新的方法和属性
前置增强:
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory; import java.lang.reflect.Method; public class Main{
public static void main(String[] args) {
//1、不使用xml配置实例化
Worker target=new DigWorker();
BeforeAdvice advice=new WearingBeforeAdvice();
//Spring提供的代理工厂
ProxyFactory pFactory=new ProxyFactory();
// //指定做优化,又将使用cglib动态
// pFactory.setOptimize(true);
//
// //指定对接口进行代理,通过JDK动态代理
// pFactory.setInterfaces(target.getClass().getInterfaces()); //设置代理目标,默认使用Cglib动态代理
pFactory.setTarget(target); //为代理目标添加前置增强,这里会为目标的每个方法都增加增强
pFactory.addAdvice(advice);
//生成代理实例
Worker proxy=(Worker)pFactory.getProxy();
proxy.getTool("钻机");
proxy.digSomething("煤炭");
proxy.initWork();
}
} interface Worker {
void getTool(String tool);
void digSomething(String type);
void initWork();
} class DigWorker implements Worker{ @Override
public void getTool(String tool) {
System.out.println("---------------调用方法--------------");
System.out.println("领取工具:"+tool);
// throw new RuntimeException("运行异常");
} @Override
public void digSomething(String type) {
System.out.println("---------------调用方法--------------");
System.out.println("开始卖力挖取:"+type); } @Override
public void initWork() {
System.out.println("生成一个DigWorker实例");
}
} class WearingBeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
if(arg1.length>0) {
String thing = (String) arg1[0];
System.out.println("--------------进行前置增强---------------");
System.out.println("得到" + thing + "之前先穿上工作服!!");
}else{
System.out.println("--------------进行前置增强2---------------");
}
}
}
--------------进行前置增强---------------
得到钻机之前先穿上工作服!!
---------------调用方法--------------
领取工具:钻机
--------------进行前置增强---------------
得到煤炭之前先穿上工作服!!
---------------调用方法--------------
开始卖力挖取:煤炭
--------------进行前置增强2---------------
生成一个DigWorker实例
前置和后置:
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.lang.reflect.Method; public class Main{
public static void main(String[] args) {
//2、下面使用Spring的xml配置实例化
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
//worker1带前置和后置增强
Worker worker1=(Worker)ctx.getBean("worker1");
System.out.println("worker1进入工作:");
worker1.getTool("锄头");
}
} interface Worker {
void getTool(String tool);
void digSomething(String type);
void initWork();
} class DigWorker implements Worker{ @Override
public void getTool(String tool) {
System.out.println("---------------调用方法--------------");
System.out.println("领取工具:"+tool);
// throw new RuntimeException("运行异常");
} @Override
public void digSomething(String type) {
System.out.println("---------------调用方法--------------");
System.out.println("开始卖力挖取:"+type); } @Override
public void initWork() {
System.out.println("生成一个DigWorker实例");
}
} class WearingBeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
if(arg1.length>0) {
String thing = (String) arg1[0];
System.out.println("--------------进行前置增强---------------");
System.out.println("得到" + thing + "之前先穿上工作服!!");
}else{
System.out.println("--------------进行前置增强2---------------");
}
}
} class WeaningAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("--------------进行后置增强---------------");
System.out.println("工作结束,请先脱掉工作服");
}
}
benas文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 要进行增强的目标 -->
<bean id="target" class="com.company.DigWorker" init-method="initWork"/> <!-- 前置增强的方法 -->
<bean id="beforeAdvice" class="com.company.WearingBeforeAdvice" />
<!-- 后置增强的方法 -->
<bean id="afterAdvice" class="com.company.WeaningAfterAdvice" /> <!--同时设置前置增强和后置增强方法 -->
<bean id="worker1" class="org.springframework.aop.framework.ProxyFactoryBean"
p:proxyInterfaces="com.company.Worker" p:interceptorNames="beforeAdvice,afterAdvice"
p:target-ref="target" lazy-init="true"/>
</beans>
Result:
生成一个DigWorker实例
worker1进入工作:
--------------进行前置增强---------------
得到锄头之前先穿上工作服!!
---------------调用方法--------------
领取工具:锄头
--------------进行后置增强---------------
工作结束,请先脱掉工作服
环绕增强:
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.aopalliance.intercept.MethodInterceptor; public class Main{
public static void main(String[] args) {
//2、下面使用Spring的xml配置实例化
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
//work2带环绕增强
Worker worker2=(Worker)ctx.getBean("worker2");
System.out.println("worker2进入工作:");
worker2.getTool("钻机");
}
} interface Worker {
void getTool(String tool);
void digSomething(String type);
void initWork();
} class DigWorker implements Worker{ @Override
public void getTool(String tool) {
System.out.println("---------------调用方法--------------");
System.out.println("领取工具:"+tool);
// throw new RuntimeException("运行异常");
} @Override
public void digSomething(String type) {
System.out.println("---------------调用方法--------------");
System.out.println("开始卖力挖取:"+type); } @Override
public void initWork() {
System.out.println("生成一个DigWorker实例");
}
} class WearingInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
Object[] args=arg0.getArguments();//目标方法入参
String toolname=(String)args[0];
System.out.println("--------------环绕增强开始---------------");
System.out.println("得到"+toolname+"之前先穿上工作服");
Object object=arg0.proceed();
System.out.println("工作结束,请先脱掉工作服");
System.out.println("--------------环绕增强结束---------------");
return object;
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 要进行增强的目标 -->
<bean id="target" class="com.company.DigWorker" init-method="initWork"/>
<!-- 环绕增强的方法 -->
<bean id="aroundAdvice" class="com.company.WearingInterceptor" />
<!--设置环绕增强方法 -->
<bean id="worker2" class="org.springframework.aop.framework.ProxyFactoryBean"
p:proxyInterfaces="com.company.Worker" p:interceptorNames="aroundAdvice"
p:target-ref="target" lazy-init="true"/>
</beans>
Result:
生成一个DigWorker实例
worker2进入工作:
--------------环绕增强开始---------------
得到钻机之前先穿上工作服
---------------调用方法--------------
领取工具:钻机
工作结束,请先脱掉工作服
--------------环绕增强结束---------------
异常抛出增强:
import org.springframework.aop.ThrowsAdvice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.lang.reflect.Method; public class Main{
public static void main(String[] args) throws Exception {
//2、下面使用Spring的xml配置实例化
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
//work3带环绕增强
Worker worker3=(Worker)ctx.getBean("worker3");
System.out.println("worker3进入工作:");
worker3.getTool("锤子");
}
} interface Worker {
void getTool(String tool);
void digSomething(String type);
void initWork();
} class DigWorker implements Worker{ @Override
public void getTool(String tool) {
System.out.println("---------------调用方法--------------");
System.out.println("领取工具:"+tool);
throw new RuntimeException("运行异常");
} @Override
public void digSomething(String type) {
System.out.println("---------------调用方法--------------");
System.out.println("开始卖力挖取:"+type); } @Override
public void initWork() {
System.out.println("生成一个DigWorker实例");
}
} class WeaningThrowAdvice 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("成功回滚事务");
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 要进行增强的目标 -->
<bean id="target" class="com.company.DigWorker" init-method="initWork"/> <!-- 异常抛出增强的方法 -->
<bean id="throwAdvice" class="com.company.WeaningThrowAdvice" /> <!--设置异常抛出增强方法 proxyTargetClass="true",使用CgLib代理-->
<bean id="worker3" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="throwAdvice"
p:target-ref="target"
p:proxyTargetClass="true"/>
</beans>
Result:
生成一个DigWorker实例
worker3进入工作:
---------------调用方法--------------
领取工具:锤子
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.company.DigWorker$$EnhancerBySpringCGLIB$$b3caca85.getTool(<generated>)
at com.company.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.IllegalAccessException: Class org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor can not access a member of class com.company.WeaningThrowAdvice with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
at java.lang.reflect.Method.invoke(Method.java:491)
at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invokeHandlerMethod(ThrowsAdviceInterceptor.java:145)
at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:130)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
http://blog.csdn.net/icarus_wang/article/details/51737474
http://www.kancloud.cn/evankaka/springlearning/119668
AOP 增强方法的更多相关文章
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
- spring5 源码深度解析----- AOP目标方法和增强方法的执行(100%理解AOP)
上一篇博文中我们讲了代理类的生成,这一篇主要讲解剩下的部分,当代理类调用时,目标方法和代理方法是如何执行的,我们还是接着上篇的ReflectiveMethodInvocation类Proceed方法来 ...
- 使用spring:aop中修改增强方法中的参数
大家都知道,在增强方法中,使用jp.getArgs()[index]可以获取传进来的参数,但是参数传进来之后,怎么改变它的值呢? 因为jp.getArgs()[index]获取到的只是数据的备份,所以 ...
- spring源码-aop增强-5.2
一.aop增强就是针对于不同的切面进行的相关增强,目的当然是更好的支持相关应用和解耦. 二.默认的aop增强类有AspectJMethodBeforeAdvice.AspectJMethodBefor ...
- AOP获取方法注解实现动态切换数据源
AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...
- Spring AOP增强(Advice)
Sring AOP通过PointCut来指定在那些类的那些方法上织入横切逻辑,通过Advice来指定在切点上具体做什么事情.如方法前做什么,方法后做什么,抛出异常做什么. Spring中有两种方式定义 ...
- JS实现AOP拦截方法调用
//JS实现AOP拦截方法调用function jsAOP(obj,handlers) { if(typeof obj == 'function'){ obj = obj.prot ...
- 【Eclipse】_Eclipse自动补全增强方法 & 常用快捷键
一,Eclipse自动补全增强方法 在Eclipse中,从Window -> preferences -> Java -> Editor -> Content assist - ...
- AOP记录方法的执行时间
作用AOP监控方法的运行时间如下: @Component @Aspect public class LogAop { private Logger log = LoggerFactory.getLog ...
随机推荐
- java基础---->java调用oracle存储过程
存储过程是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.今天 ...
- iconfont阿里爸爸做的开源图库
iconfont 三种使用姿势 1.unicode格式 优点 兼容性最好,支持ie6+ 支持按字体的方式去动态调整图标大小,颜色等等 缺点 不支持多色图标 在不同的设备浏览器字体的渲染会略有差别,在不 ...
- Service简介 demos
extends:http://blog.csdn.net/ithomer/article/details/7364024 一. Service简介 Service是android 系统中的四大组件之一 ...
- linux查看日志文件内容命令tail、cat、tac、head、echo、vi
linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...
- 【webpack】---模块打包机webpack基础使用---【巷子】
001.什么是webpack? 作用有哪些? WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,Ty ...
- 使用DBMS_SUPPORT包
DBMS_SUPPORT包从Oracle7.2引入,主要功能用以提供更完整的用户session跟踪信息,这个包可以通过运行 dbmssupp.sql 和 prvtsupp.plb 两个脚本来创建.该脚 ...
- Andrew Ng机器学习公开课笔记 -- Logistic Regression
网易公开课,第3,4课 notes,http://cs229.stanford.edu/notes/cs229-notes1.pdf 前面讨论了线性回归问题, 符合高斯分布,使用最小二乘来作为损失函数 ...
- 商铺项目(Logback配置与使用)
<?xml version="1.0" encoding="utf-8"?> <configuration debug="false ...
- MVC模式:python案例
quotes = ('A man is not complete until he is married. Then he is finished.', 'As I said before, I ne ...
- windows 系统无法启动windows event log 服务
windows 系统无法启动windows event log 服务 关键词:无法启动系统事件日志 尝试解决步骤 [1]权限:把如图中logsfile文件等都给local service [2]把C: ...