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

<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. HihoCoder - 1103 Colorful Lecture Note

    Little Hi is writing an algorithm lecture note for Little Ho. To make the note more comprehensible, ...

  2. VB中将类标记为可序列化

    引用名空间: Imports SystemImports System.Runtime.Serialization 在类前加特性: <Serializable> 更多内容: https:/ ...

  3. List Leave

    本次作业是建立二叉树并输出叶结点 (1)首先是定义结点,包括左孩子,右孩子 typedef struct { int lch;//左孩子 int rch;//右孩子 }Node; (2)建立二叉树 c ...

  4. char对比varchar

    char对比varchar 相同点:char与varchar都是存储字符串的数据类型 不同点:char是固定长度的字符类型,而varchar是可变长度的字符类型,这个一定要注意.另外进行select时 ...

  5. DWR使用总结

      这两天学了下DWR,现在总结一下. DWR是方便使用AJAX连接JS和JAVA的的一个框架,把服务器端 Java 对象的方法公开给 JavaScript 代码. 如果是用dwr2.0的jar包,还 ...

  6. Springboot & Mybatis 构建restful 服务五

    Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...

  7. Lambda根据属性名字选择或筛选

    using System; using System.Linq.Expressions; internal class LambdaHelper { /// <summary> /// 指 ...

  8. js生成tree形组织机构下拉框

    1.首先我们正常数据是如下所示: [ { id: 1, pid: 0, name: '公司组织' }, { id: 2, pid: 1, name: '总经办' }, { id: 3, pid: 1, ...

  9. 用python turtle实现汉诺塔的移动

    1.汉诺塔 汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按大小 ...

  10. 2019.03.29 NOIP训练 友好国度(点分治+容斥)

    传送门 思路: 直接上点分治+容斥计算每个因数对应的贡献即可. 代码: #include<bits/stdc++.h> #define ri register int using name ...