三、自动代理的实现

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命令空间的更多相关文章

  1. Spring只定义接口自动代理接口实现类

    能够扫描到包 @ComponentScan("org.zxp.esclientrhl") ESCRegistrar类主要实现ImportBeanDefinitionRegistra ...

  2. Spring框架学习08——自动代理方式实现AOP

    在传统的基于代理类的AOP实现中,每个代理都是通过ProxyFactoryBean织入切面代理,在实际开发中,非常多的Bean每个都配置ProxyFactoryBean开发维护量巨大.解决方案:自动创 ...

  3. AOP的自动代理

    Spring的aop机制提供两类方式实现类代理.一种是单个代理,一种是自动代理. 单个代理通过ProxyFactoryBean来实现(就如上面的配置). 自动代理:自动代理能够让切面定义来决定那个be ...

  4. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  5. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  8. spring aop自动代理xml配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. spring aop自动代理注解配置之二

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

随机推荐

  1. RVM 解决 Ruby 的版本问题

    RVM 是一个命令行工具,可以提供一个便捷的多版本 Ruby 环境的管理和切换. RVM 的官网是 https://rvm.io/. 如果你打算学习 Ruby / Rails, RVM 是必不可少的工 ...

  2. 喝咖啡写脚本,顺便再加一点点CSS语法糖 1.选择环境

    经过对前端开发的初步了解,大体上发现了以下几点,前端开发需要使用脚本语言,主要是JavaScript,需要Html,需要CSS,这些东西相信很多人已经很熟了.但是仅仅只是学习一点简单的JS,配合Htm ...

  3. Lua语言的特别之处

    所谓特别,是相对的,是相对别的主流语言而言,有些也可能只是我个人看法. 1. 函数定义与调用,与代码位置的先后顺序有关,例如 calculate() function calculate() .... ...

  4. CSS3之过渡Transition

    CSS3中的过渡Transition有四个中心属性:transition-property.transition-duration.transition-delay和transition-timing ...

  5. linq入门系列导航

    写在前面 为什么突然想起来学学linq呢?还是源于在跟一个同事聊天的时候,说到他们正在弄得一个项目,在里面用到了linq to sql.突然想到距上次使用linq to sql是三年前的事情了.下班回 ...

  6. angular_$inject

    <!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> &l ...

  7. java通过地址获取主机名

    关键代码: try { String str=Chat.getJt().getText().toString();//获取输入内容 String[] ipstr=str.split("[.] ...

  8. log4j1 插入mysql

    做任务需要用到这样的需求: 使用log4j 1.2进行日志管理. 将日志输出到mysql中 系统数据库表利用脚本每日生成一张,插入系统运行时特定的表中. 实现方法 properties(放在项目根目录 ...

  9. 我眼中的Android IDE

    我作为一个Android小白,首先跟Android打交道的就是它的IDE(Integrated Development Environment,集成开发环境)了. 记得刚开始时是从图书馆借了本Andr ...

  10. 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CurrencyClientServe

    有的说: 在Servers视图里双击创建的server,然后在其server的配置界面中选中"Publish module contexts to separate XML files&qu ...