Spring 通知和顾问进行增强
使用顾问增加前置增强和后置增强
<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 通知和顾问进行增强的更多相关文章
- Spring 通知(Advice)和顾问(Advisor)
AOP ( Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring通知类型及使用ProxyFactoryBean创建AOP代理
Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Ad ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理
通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...
- spring7——AOP之通知和顾问
通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...
- spring-AOP之通知和顾问
通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...
- Spring横切面(advice),增强(advisor),切入点(PointCut)(转)
Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解: 1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理 ...
- Spring AOP获取不了增强类(额外方法)或无法通过getBean()获取对象
Spring AOP获取不了增强类(额外方法)和无法通过getBean()获取对象 今天在学习AOP发现一个小问题 Spring AOP获取不了额外方法,左思右想发现是接口上出了问题 先上代码 获取不 ...
- Spring通知,顾问,增强
1.AOP (Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编 ...
- Spring学习(七)——增强类
Spring 切点 什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物.但在这为数从多的连接点中,如何定位 ...
随机推荐
- AX2009 批处理作业中使用多线程---独立任务模式
每个工单独立一个任务. Class /* 独立任务模式 */ class DemoBatchIndividualTasks extends RunBaseBatch { str 20 SalesOrd ...
- function 函数
function:函数体内部的语句在执行时,一旦执行到return时,函数就执行完毕,并将结果返回.因此,函数内部通过条件判断和循环可以实现非常复杂的逻辑. 如果没有return语句,函数执行完毕后也 ...
- 电商项目-商品表(spu)、规格表(sku)设计
之前在工作中,需要实现商品规格功能,做了很长一段时间,现在回过头来整理下设计思路. sku,spu概念: SPU = Standard Product Unit (标准化产品单元),SPU是商品信息聚 ...
- java多线程系列 目录
Java多线程系列1 线程创建以及状态切换 Java多线程系列2 线程常见方法介绍 Java多线程系列3 synchronized 关键词 Java多线程系列4 线程交互(wait和 ...
- paxos made more simple
paxos算法是进入分布式领域的一块基石,有关paxos的讨论有很多精彩的详细论述,很多牛人不惜宝贵时间以大幅详尽段落叙述.感谢他们,paxos more simple 理解paxos前,我建议以面到 ...
- linux中tomcat startup.sh出现commond not found
问题: 前些天,再Linux提交更新代码启动tomcat时报commond not found 过程: 查了下百度,http://code2care.org/2015/-bash:-startup.s ...
- XML文件的解析—DOM、SAX
一.DOM 解析 思路:获得Document对象,遍历其中节点获得需要的内容 要点: Document : DocuemntBuilderFactory --newDocumentBuilder - ...
- ThinkPhp5 出现访问出现 No input file specified. 问题
今天复习一下ThinkPhp5,在官网下载了核心版,windows下配置了虚拟域名之后出现了神奇的现象 如下图 直接访问域名能访问到index模块下的index控制器下的index方法 但是我输入完整 ...
- Https,Http,TCP,IP的一些理解
网络模型分为7层,应用层,表现层,会话层,传输层,网络层,链路层,物理层,每一层有很多不同的协议. http:属于应用层的协议,负责的是数据以什么结构传输也可以说成是打包成什么样子 SSL/TLS:属 ...
- BAT:文件中替换字符(保留空格和换行)
@echo off rem CMD:"color --help" :: setting color:back white,content blue color f9 ::old s ...