Spring AOP使用整理:自动代理以及AOP命令空间
三、自动代理的实现
1、使用BeanNameAutoProxyCreator
通过Bean的name属性自动生成代理Bean。
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Target</value> <!-- 名字以Target结尾的bean将被拦截 -->
</list>
</property>
<property name="interceptorNames">
<list>
<value>personAroundAdvice</value>
</list>
</property>
</bean>
2、使用DefaultAdvisorAutoProxyCreator
DefaultAdvisorAutoProxyCreator允许我们只需定义相应的Advisor通知者,就可以完成自动代理。配置好DefaultAdvisorAutoProxyCreator受管Bean后,它会自动查找配置文件中定义的Advisor,并将它们作用于所有的Bean。
Spring提供了以下通知者:
RegexpMethodPointcutAdvisor
NameMatchMethodPointcutAdvisor
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <bean id="nameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="personAroundAdvice"/>
<property name="mappedNames">
<list>
<value>*info*</value>
</list>
</property>
</bean>
3、测试代码
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:com/cjm/aop/beans.xml");
Person p = (Person)context.getBean("personTarget"); //此处获取的是具体的Bean,而不是代理Bean
p.info();
四、基于aop命名空间的AOP
1、切面类MyAspect的源码
public class MyAspect {
public void BeforeAdvice(JoinPoint point){
PersonImpl p = (PersonImpl)point.getTarget();
}
public void AfterAdvice(JoinPoint point){
PersonImpl p = (PersonImpl)point.getTarget();
}
public void AfterReturningAdvice(Object retValue){
System.out.println(retValue);
}
public void AroundAdvice(ProceedingJoinPoint point)throws Throwable{
System.out.println("AroundAdvice >> 目标对象 前");
point.proceed(point.getArgs());
System.out.println("AroundAdvice >> 目标对象 后");
}
public void ThrowingAdvice(Throwable throwing)throws Throwable{
System.out.println(throwing);
}
}
2、配置
<!-- 切面受管Bean -->
<bean id="myAspect" class="com.cjm.aop.MyAspect"/> <aop:config proxy-target-class="true">
<!-- 声明一个切面 -->
<aop:aspect ref="myAspect" order="0">
<!-- 声明一个切入点 -->
<aop:pointcut id="pointcut1" expression="execution(* com.cjm.aop..*(..))"/> <!-- 声明通知 -->
<aop:before pointcut-ref="pointcut1" method="BeforeAdvice"/>
<aop:after pointcut-ref="pointcut1" method="AfterAdvice"/>
<aop:after-returning pointcut-ref="pointcut1" method="AfterReturningAdvice" returning="retValue"/>
<aop:around pointcut-ref="pointcut1" method="AroundAdvice"/>
<aop:after-throwing pointcut-ref="pointcut1" method="ThrowingAdvice" throwing="throwing"/>
</aop:aspect>
</aop:config>
Spring AOP使用整理:自动代理以及AOP命令空间的更多相关文章
- Spring只定义接口自动代理接口实现类
能够扫描到包 @ComponentScan("org.zxp.esclientrhl") ESCRegistrar类主要实现ImportBeanDefinitionRegistra ...
- Spring框架学习08——自动代理方式实现AOP
在传统的基于代理类的AOP实现中,每个代理都是通过ProxyFactoryBean织入切面代理,在实际开发中,非常多的Bean每个都配置ProxyFactoryBean开发维护量巨大.解决方案:自动创 ...
- AOP的自动代理
Spring的aop机制提供两类方式实现类代理.一种是单个代理,一种是自动代理. 单个代理通过ProxyFactoryBean来实现(就如上面的配置). 自动代理:自动代理能够让切面定义来决定那个be ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- spring aop自动代理xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring aop自动代理注解配置之二
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
随机推荐
- WampServer下使用多端口访问
因为学习中要用到跨域请求,所以不得不在wamp集成环境下添加多站点服务. 1.首先你要确保已经正确安装了wamp. 2.接着在wamp的安装目录下找到Apache2的httpd.conf文件,比如我的 ...
- 【BZOJ1008】【HNOI2008】越狱(数学排列组合题)
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3140 Solved: 1317[Submit][Status] ...
- Javascript基础系列之(六)循环语句(do while循环)
do/while 循环是 while 循环的变体.该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环. 语法结构如下 do { statement } while ...
- Javascript基础系列之(七)函数(定义和调运函数)
函数是一个可以随时运行的语句,简单说,函数是完成某个功能的一组语句,它接受0或者多个参数. 函数的基本语法如下 function functionName([arg0,arg1,......argN] ...
- 小记:事务(进程 ID 56)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。
今天在做SQL并发UPDATE时遇到一个异常:(代码如下) //Parallel 类可产生并发操作(即多线程) Parallel.ForEach(topics, topic => { //DBH ...
- 调研Android平台开发环境的发展演变
Android是Google推出的开源手机操作系统,主要以开发应用为主,要进行Android开发首先得搭建好开发平台.最近在搭建Android的开发环境,发现往往一个小问题都能花费你大半天时间,从刚开 ...
- VS中两个常用辅助工具
一. 首当推荐的是DPack 下载地址:http://www.usysware.com/dpack/ 快捷键:以下都是个人常用的热键.其他还有,我都用得比较少了,3个已经完全够了 Alt+U 查找 ...
- 【CodeForces 312B】BUPT 2015 newbie practice #3A Archer
题 SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the targ ...
- 思维导图XMiand
XMiand: 异常强大的国产思维导图工具,还能将图同步到服务器上.做思维导图和头脑风暴必备软件,还能转换绘制鱼骨图.二维图.树形图.逻辑图.组织结构图.
- 数组实现栈的结构(java)
自定义数组实现栈的结构. package test; public class MyArrayStackClient { public static void main(String[] args) ...