使用顾问增加前置增强和后置增强

<bean id="1" class="目标对象"></bean>

<bean id="2" class="代理对象"></bean>

<bean id="3" class="顾问">
<property name="5" ref="代理对象id">
<property name="6" value="匹配的规则">
</bean>

<bean id="4" class="代理工厂Bean">
<property name="7" ref="目标对象">
<property name="8" value="顾问id">
</bean>

前置增强:

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.xsd">
<!--实现类(目标对象)-->
<bean id="SSI" class="cn.Spring.Day16Advisor.ServiceSqlImpl"></bean>
<!--增强类(代理对象)-->
<bean id="MMBA" class="cn.Spring.Day16Advisor.MyMethodBeforeAdvice"></bean>
<!--顾问-->
<bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="MMBA"></property><!--传的是代理对象-->
<property name="mappedNames" value="*s*"></property><!--匹配的方法-->
</bean> <!--代理工厂Bean-->
<bean id="MyProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="SSI"></property><!--接收目标对象-->
<property name="interceptorNames" value="advisor"></property><!--然后把顾问拿进来-->
</bean>
</beans>

测试方法代码:

import cn.Spring.Day16Advisor.IServiceSql;
import cn.Spring.Day16Advisor.ServiceSqlImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test20181124 {
@Test
public void test1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationAdvisor.xml");
IServiceSql bean =(IServiceSql) applicationContext.getBean("MyProxy");//传入代理工厂Bean的id
bean.delete();
bean.insert();
bean.select();
bean.update();
}
}

代理类:

前置加后置增强:

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.xsd">
<!--实现类(目标对象)-->
<bean id="SSI" class="cn.Spring.Day16Advisor.ServiceSqlImpl"></bean>
<!--增强类(代理对象)-->
<bean id="MMBA" class="cn.Spring.Day16Advisor.MyMethodBeforeAdvice"></bean>
<!--顾问-->
<bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="MMBA"></property><!--传的是代理对象-->
<property name="mappedNames" value="*s*"></property><!--匹配的方法-->
</bean> <!--增强类(代理对象)-->
<bean id="MARA" class="cn.Spring.Day16Advisor.MyAfterReturningAdvice"></bean>
<!--顾问-->
<bean id="advisorT" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="MARA"></property><!--传的是代理对象-->
<property name="mappedNames" value="*s*"></property><!--匹配的方法-->
</bean>
<!--代理工厂Bean-->
<bean id="MyProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="SSI"></property><!--接收目标对象-->
<property name="interceptorNames" value="advisor,advisorT"></property><!--然后把顾问拿进来-->
</bean>
</beans>

需要注意的是在代理工厂Bean中interceptorNames的value值要多个的话使用逗号隔开。

结果:

Spring 通知和顾问进行增强的更多相关文章

  1. Spring 通知(Advice)和顾问(Advisor)

    AOP ( Aspect  Oriented Programming  面向切面编程)  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...

  2. Spring通知类型及使用ProxyFactoryBean创建AOP代理

    Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Ad ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

  4. spring7——AOP之通知和顾问

    通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...

  5. spring-AOP之通知和顾问

    通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...

  6. Spring横切面(advice),增强(advisor),切入点(PointCut)(转)

    Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解: 1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理 ...

  7. Spring AOP获取不了增强类(额外方法)或无法通过getBean()获取对象

    Spring AOP获取不了增强类(额外方法)和无法通过getBean()获取对象 今天在学习AOP发现一个小问题 Spring AOP获取不了额外方法,左思右想发现是接口上出了问题 先上代码 获取不 ...

  8. Spring通知,顾问,增强

    1.AOP  (Aspect  Oriented Programming  面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编 ...

  9. Spring学习(七)——增强类

    Spring 切点 什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物.但在这为数从多的连接点中,如何定位 ...

随机推荐

  1. Swift.Operator-and-Items-in-Swift(1)

    Operator and Item 1. ..< a for-in loop and the half-open range operator (..<) // Check each pa ...

  2. 第一次面试经历(hr面)

    经过介绍,我有幸去到一家国际背景的广告公司面试前端开发实习生.收到的邮件是复试通知,看来我已经跳过了第一轮面试. 来到hr请我进了一个小间坐下里填求职书,里面有各种个人信息,有兴趣爱好,有工作经历,以 ...

  3. select 的选中问题

    function bind(pageIndex) { if (getQueryString("_status") == "3") {//从首页中慢病管理人数进入 ...

  4. Percona XtraBackup使用说明(转)

    Percona XtraBackup使用说明 转载出自: https://blog.csdn.net/wfs1994/article/details/80396604 XtraBackup介绍 Per ...

  5. SVN与资源库同步后动作的字母意义

    U:表示从服务器收到文件更新了G:表示本地文件以及服务器文件都以更新,而且成功的合并了A:表示有文件或者目录添加到工作目录R:表示文件或者目录被替换了C:表示文件的本地修改和服务器修改发生冲突

  6. Java容器-个人整理1

    1.初始化集合时,若能知道知道容量,尽量初始化时确定容量.容器类一般可以自动扩充,但扩充是有性能代价的. 2.Arrays.asList()的底层表示仍然时数组,因此不能进行调整尺寸的操作. 3.Ha ...

  7. Jmeter多用户利用集合点瞬压并发测试

    在测试一些限时秒杀类似的接口时,需要模拟多用户同时一瞬间访问接口,我们这里简单模拟多用户同时访问百度. 1.首先打开Jmeter,在测试计划下添加线程组. 2.在线程组下添加HTTP请求. 3.在HT ...

  8. jmeter本身的一个bug记录

    1.使用jmeter测http接口 2.断言接口返回的内容是否包含某串文本 3.结果:总是返回断言失败,即使接口返回的内容包含了该文本 接口返回的值为: {"code":" ...

  9. 十分钟带你读懂《增长黑客》zz

    背景 “If you are not growing, then you are dying. ”(如果企业不在增长,那么就是在衰亡!) 这句话适用于企业,也适用于个人.人生毕竟不像企业,是非成败,似 ...

  10. 配置json-server

    1.全局安装json-server[可能需要管理员权限] npm i -g json-server 2.创建文件夹jsonerver,初始化package.json文件npm init 3.局部安装j ...