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),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物.但在这为数从多的连接点中,如何定位 ...
随机推荐
- Mysql必知必会 检索数据
检索数据 SELECT 语句 为了使用SELECT检索表数据,必须至少给出两条信息--想选择什么,以及从什么地方选择 功能 语句 备注 检索单个列 SELECT col_1 FROM table_na ...
- python写注册
# coding = UTF-8 注释格式 import datetime 引用日期 today = datetime.datetime.today().strftime("%Y-%m-%d ...
- java 判断null和空
判断null和空 org.apache.commons.lang3 if(StringUtils.isBlank(valuationMeasureUnitName)){ }
- [转] Shader Blending
引用:1.Unity3D shader Blending2.[风宇冲]Unity3D教程宝典之Shader篇:第十三讲 Alpha混合 混合(Blending)是计算机呈现渲染结果的最后阶段,每一个像 ...
- python搭建服务器时nginx的有关问题
最近在学习Python服务器搭建的内容,网上大多是Windows环境下的,由于我使用的是Mac,为了不想装双系统折腾,就只好一步步采坑了.比较基础的我一步步记录下来, 1.安装nginx: brew ...
- oracle分区表的使用和查询
本文参考了 https://blog.csdn.net/mzglzzc/article/details/46300645 一 创建和使用分区表 1.范围分区(RANGE)范围分区将数据基于范围映射到 ...
- JavaWeb(一)-Servlet中的Config和Context
一.ServletConfig对象 1.1获取一个servletConfig对象 1)通过初始化方法获得一个servletconfig 2)通过继承父类(GenericServlet.)得到一个ser ...
- centos7搭建Cisco上网方式
1.下载脚本 wget https://git.io/vpnsetup-centos -O vpnsetup.sh 2.修改 vi vpnsetup.sh 替换为你自己的值: YOUR_IPSEC_P ...
- Qt5对XML文件操作
转自https://blog.csdn.net/hpu11/article/details/80227093 写入xml //写xml void WriteXml() { //打开或创建文件 QFil ...
- rem 是如何实现自适应布局的
摘要:rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个px字号,则可以来算出元素的宽高.本文讲的是如何使用rem实现自适应.· rem这是个低调的css单位,近一 ...