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),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物.但在这为数从多的连接点中,如何定位 ...
随机推荐
- java 日志体系(四)log4j 源码分析
java 日志体系(四)log4j 源码分析 logback.log4j2.jul 都是在 log4j 的基础上扩展的,其实现的逻辑都差不多,下面以 log4j 为例剖析一下日志框架的基本组件. 一. ...
- idea实现热部署并且开启自动编译
[注]本文转自https://blog.csdn.net/z15732621582/article/details/79439359博文,如有冒犯,请联系博主: 问题描述: 最近在调试代码并进行本地测 ...
- linux简单安装方法
一.配置静态IP NAT:模式: 修改网卡eth0 vim /etc/sysconfig/network-scripts/ifcfg-eth0 内容如下: DEVICE=eth0 HWADDR=:0C ...
- 在Java的Condition接口【唤醒全部线程】
在Java的Condition接口中,存在的几个方法跟Synchronized中的wait(),waitall(),wait(time ^),这个几个方法一一对应起来,但是在Lock.newCondi ...
- 如何高效的学习 TensorFlow 代码?
https://www.zhihu.com/question/41667903 Linux[公共基础]:TensorFlow的主要运行平台之一就是Linux,但是正式版对Windows的支持日趋完善, ...
- Mac 安装配置Jenkins+github完成项目构建
Jenkins Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建.测试和部署软件.Jenkins 支持各种运行方式,可通过系统包, Docker 或者通过一个独立的 J ...
- 通俗易懂--SVM算法讲解(算法+案例)
1.SVM讲解 新闻分类案例 SVM是一个很复杂的算法,不是一篇博文就能够讲完的,所以此篇的定位是初学者能够接受的程度,并且讲的都是SVM的一种思想,通过此篇能够使读着会使用SVM就行,具体SVM的推 ...
- PHP配置文件详解php.ini
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...
- laravel 打印完整sql
DB::connection()->enableQueryLog(); // 开启QueryLog \App\User::find(1); dump(DB::getQueryLog());
- 深入浅出Git教程【转载】转载
深入浅出Git教程(转载) 目录 一.版本控制概要 1.1.什么是版本控制 1.2.常用术语 1.3.常见的版本控制器 1.4.版本控制分类 1.4.1.本地版本控制 1.4.2.集中版本控制 1 ...