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模式也即模板模式,用于对一些不太变化 ...
随机推荐
- 分享一个酷炫动态登录页面html
话不多说,如下图: 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <H ...
- mongo shell 通过返回信息定位错误点
有时候我们会通过mongo shell 运行一些脚本,去执行更新或运维需求.mongo shell 可执行的代码可以实现比较复杂的功能,代码也可以比较丰富.当执行报错时,如果可以快速定位到错误点,对解 ...
- python 基础学习笔记(5)--文件操作
**python 的文件操作** - [ ] 使用python来读写文件是非常简单的操作,我们使用open()来打开一个文件,获取到文件的语柄,然后通过文件语柄就可以进行各种各样的操作了. - [ ] ...
- PostgreSQL中的onflict
PostgreSQL 9.5 引入了一项新功能,UPSERT(insert on conflict do),当插入遇到约束错误时,直接返回,或者改为执行UPDATE. 1.不存在则插入,存在则更新 i ...
- 关于在maven项目中配置文件资源导出问题
标准的Maven项目都会有一个resources目录来存放我们所有的资源配置文件,但是我们往往在项目中不仅仅会把所有的资源配置文件都放在resources中,同时我们也有可能放在项目中的其他位置,那么 ...
- 你必须知道的Dockerfile
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 一.关于Dockerfile 在Docker中创建镜像最常用的方式,就是使用D ...
- Vue 03
目录 组件 组件的分类 组件的特点 组件的使用 组件传参-父传子 组件传参-子传父 组件 组件就是html, css和js文件的集合体, 实现对代码的复用, 组件就是vue对象 组件的分类 根组件 & ...
- Python实战——基于股票的金融数据量化分析
说明:本文只是通过自己的已学知识对股票数据进行了一个简单的量化分析,只考虑了收盘情况,真实的量化交易中仅仅考虑收盘情况是不够的,还有很多的复杂因素,而且仅仅三年数据是不足以来指导真实的股票交易的,因此 ...
- HelloDjango 第 11 篇:自动生成文章摘要
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 博客文章的模型有一个 excerpt 字段,这个字段用于存储文章的摘要.目前为止,还只 ...
- cookie及其特点
关于cookie我们首先要知道cookie是指会话跟踪技术 我们可以用它来做一下事情,但是我们需要清楚cookie是不安全的 功能: 会话状态管理(如用户登录状态.购物车.游戏分数和其它需要记录的信息 ...