Spring的增强模式
一、前置增强
1、IdoSomeService
    
2、IdoSomeServiceImpl类实现IdoSomeService接口
    
3、MyBeforeAdvice 实现前置增强方法
    
4、applicationContext.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:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--代理工厂增强-->
<!--注入业务Bean-->
<bean id="idoSomeService1" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myBeforeAdvice" class="cn.spring.proxyfactory.MyBeforeAdvice"></bean>
<!--使用代理工厂实现增强-->
<bean id="proxyFactory1" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService1"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myBeforeAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 即JDK动态代理,但是当目标对象没有接口时,自动改为CGLIB-->
<property name="proxyTargetClass" value="true"></property>
</bean>
</beans>
5、测试类
    
6、控制台
    
二、环绕增强
1、IdoSomeService
    
2、IdoSomeServiceImpl类实现IdoSomeService接口
    
3、MyAroundAdvice 实现环绕增强方法
    
4、applicationContext.xml配置文件
<!--环绕增强实现-->
<!--注入业务Bean-->
<bean id="idoSomeService2" class="cn.spring.around.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myAroundAdvice" class="cn.spring.around.MyAroundAdvice"></bean>
<!--使用代理工厂实现增强-->
<bean id="proxyFactory2" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService2"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myAroundAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 即JDK动态代理,但是当目标对象没有接口时,自动改为CGLIB-->
<property name="proxyTargetClass" value="true"></property>
</bean>
5、测试类
    
6、控制台
    
三、异常增强
1、IdoSomeService
    
2、IdoSomeServiceImpl类实现IdoSomeService接口
    
3、MyThrowAdvice实现异常增强
    
4、applicationContext.xml配置文件
<!--异常增强实现-->
<!--注入业务Bean-->
<bean id="idoSomeService3" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myThrowAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
<!--使用代理工厂实现增强-->
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService3"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myThrowAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 即JDK动态代理,但是当目标对象没有接口时,自动改为CGLIB-->
<property name="proxyTargetClass" value="true"></property>
</bean>
5、测试类
   
6、控制台
   
四、最终增强
1、IdoSomeService
    
2、IdoSomeServiceImpl类实现IdoSomeService接口
    
3、MyThrowAdvice实现最终增强
    
4、applicationContext.xml配置文件
<!--最终增强实现-->
<!--注入业务Bean-->
<bean id="idoSomeService4" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myThrowAdvice1" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
<aop:aspect ref="myThrowAdvice1">
<aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
<aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config>
5、测试类
    
6、控制台
   
Spring的增强模式的更多相关文章
- Spring4.1新特性——Spring MVC增强
		目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ... 
- spring通过工厂模式解决页面耦合问题
		spring通过工厂模式解决页面耦合问题 
- hyper-v安装虚拟机ubuntu 18.04 64bit后无法使能增强模式怎么办
		1.获取脚本来使能增强模式 $ sudo apt-get update $ sudo apt install git $ git clone https://github.com/jterry75/x ... 
- 跟Evan学Sprign编程思想 | Spring注解编程模式【译】
		Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ... 
- Spring AOP /代理模式/事务管理/读写分离/多数据源管理
		参考文章: http://www.cnblogs.com/MOBIN/p/5597215.html http://www.cnblogs.com/fenglie/articles/4097759.ht ... 
- Java进阶知识20 Spring的代理模式
		本文知识点(目录): 1.概念 2.代理模式 2.1.静态代理 2.2.动态代理 2.3.Cglib子类代理 1.概念 1.工厂模式 2. 单例模式 代理(Proxy ... 
- 基于XML配置的spring aop增强配置和使用
		在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ... 
- spring的代理模式
		静态代理: 首先定义一个接口,随便写一个方法 定义2个实现接口的方法 (被代理的对象) (代理对象) 需要将接口 定义get set 方法 代理增强的方法 然后实现 输出结果如下: 动态代理(jdk动 ... 
- Spring中Template模式与callback的结合使用浅析
		Spring不论是与ibatis,还是与Hibernate的结合中,都使用到了Template模式与callback技术,来达到简化代码实现的目的.Template模式也即模板模式,用于对一些不太变化 ... 
随机推荐
- 属性文件——Java&Spring
			属性文件 什么是属性文件 ? 定义:一个扩展名为properties文件,属性文件都是以key-value(键值对)来保存文件的内容,如:log4j.properties,db.properties等 ... 
- 为什么 netstat 对某些服务只显示了 tcp6 监听端口
			最近偶尔发现一个比较奇怪的现象,netstat 查看监听的服务端口时,却只显示了 tcp6 的监控, 但是服务明明是可以通过 tcp4 的 ipv4 地址访问的,那为什么没有显示 tcp4 的监听呢? ... 
- windows系统下压缩文件成tar.gz格式的方法
			tar.gz 是linux和unix下面比较常用的格式,几个命令就可以把文件压缩打包成tar.gz格式,然而这种格式在windows并不多见,WinRAR.WinZip等主流压缩工具可以释放解开,却不 ... 
- 逆向学习周记-C语言空函数
			实验环境:WIN7虚拟机 软件:VC6 首先在VC6里面写一个空函数Fun(): F7编译运行一下,没有出错,接着在函数处使用F9下断点,使程序运行到Fun函数时停下. 接着F5开始运行这个程序 程序 ... 
- 小程序--log居中  失焦获取表单中的值
			value="{{username}}" 绑定值 值在js文件的data中 pa==>Vant-Weap中表单中的值,不是双向绑定的. 你获取值后, 值并没有在对用的data ... 
- core-js@3带来的惊喜
			core-js 这个名词肯定很多人没听过,今天也是在配置babelpolyfill方法发现的 起因 在使用useBuiltIns:usage按需加载polyfill时,npm run build,就出 ... 
- Element-ui 下拉列表 选项过多时通过自定义搜索来解决卡顿问题
			当使用Select选择器时,如果下拉列表的数据量太多,会有一个明显的卡顿体验,例如: <!DOCTYPE html> <html lang="en"> &l ... 
- javascript截取字符串的最后几个字符
			在JavaScript中截取字符串一般是使用内置的substring()方法和substr()方法,这两个方法功能都很强大,也都能实现截取字符串中的最后几个字符. substring()方法 Java ... 
- 【AGC035F】Two Histograms
			Problem Description 你有一个 \(N\) 行.\(M\) 列的.每个格子都填写着 0 的表格.你进行了下面的操作: 对于每一行 \(i\) ,选定自然数 \(r_i\) (\(0\ ... 
- 使用 Docker 构建 Nebula Graph 源码
			Nebula Graph 介绍 Nebula Graph 是开源的高性能分布式图数据库.项目使用 C++ 语言开发,cmake 工具构建.其中两个重要的依赖是 Facebook 的 Thrift RP ... 
