一、前置增强

  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的增强模式的更多相关文章

  1. Spring4.1新特性——Spring MVC增强

    目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ...

  2. spring通过工厂模式解决页面耦合问题

    spring通过工厂模式解决页面耦合问题

  3. hyper-v安装虚拟机ubuntu 18.04 64bit后无法使能增强模式怎么办

    1.获取脚本来使能增强模式 $ sudo apt-get update $ sudo apt install git $ git clone https://github.com/jterry75/x ...

  4. 跟Evan学Sprign编程思想 | Spring注解编程模式【译】

    Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...

  5. Spring AOP /代理模式/事务管理/读写分离/多数据源管理

    参考文章: http://www.cnblogs.com/MOBIN/p/5597215.html http://www.cnblogs.com/fenglie/articles/4097759.ht ...

  6. Java进阶知识20 Spring的代理模式

    本文知识点(目录): 1.概念  2.代理模式      2.1.静态代理      2.2.动态代理      2.3.Cglib子类代理 1.概念 1.工厂模式  2. 单例模式 代理(Proxy ...

  7. 基于XML配置的spring aop增强配置和使用

    在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...

  8. spring的代理模式

    静态代理: 首先定义一个接口,随便写一个方法 定义2个实现接口的方法 (被代理的对象) (代理对象) 需要将接口 定义get set 方法 代理增强的方法 然后实现 输出结果如下: 动态代理(jdk动 ...

  9. Spring中Template模式与callback的结合使用浅析

    Spring不论是与ibatis,还是与Hibernate的结合中,都使用到了Template模式与callback技术,来达到简化代码实现的目的.Template模式也即模板模式,用于对一些不太变化 ...

随机推荐

  1. SQL Server解惑——为什么你的查询结果超出了查询时间范围

    废话少说,直接上SQL代码(有兴趣的测试验证一下),下面这个查询语句为什么将2008-11-27的记录查询出来了呢?这个是同事遇到的一个问题,个人设计了一个例子. USE AdventureWorks ...

  2. spring boot 加入mail邮件支持

    一.添加依赖 <!-- 邮件整合 --> <dependency> <groupId>org.springframework.boot</groupId> ...

  3. 关于jQuery MiniUI

    jQuery MiniUI v3.0 jQuery MiniUI - 专业WebUI控件库.它能缩短开发时间,减少代码量,使开发者更专注于业务和服务端,轻松实现界面开发,带来绝佳的用户体验. http ...

  4. FreeHttp1.2升级说明

    一.升级方法 下载新版本插件 https://files.cnblogs.com/files/lulianqi/FreeHttp1.2.zip  或 http://lulianqi.com/file/ ...

  5. JeeSite | 保存信息修改记录封装

    前面写过两篇关于“保存信息修改记录”的内容,分别如下: JeeSite | 保存信息修改记录 JeeSite | 保存信息修改记录续 回顾         第一篇文章通过类字段的比较返回一个有字段值不 ...

  6. 100本Python机器学习、深度学习电子书,免费送!

    此套电子书收集于网络,如有侵权请联系删除!!! 此套电子书仅用于个人学习,请勿用于商业获利,造成后果自负!!! 这套电子书包括:机器学习.深度学习.数据科学入门.神经网络等 获取资源地址:链接: ht ...

  7. Selenium(七):选择框(radio框、checkbox框、select框)

    1. 选择框 本章使用的html代码: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  8. Spring Boot + Redis实战-利用自定义注解+分布式锁实现接口幂等性

    场景 不管是传统行业还是互联网行业,我们都需要保证大部分操作是幂等性的,简单点说,就是无论用户点击多少次,操作多少遍,产生的结果都是一样的,是唯一的.而今次公司的项目里,又被我遇到了这么一个幂等性的问 ...

  9. IaaS、PaaS、SaaS介绍(非原创)

    文章大纲 一.IaaS.PaaS.SaaS介绍与比较二.参考文章 一.IaaS.PaaS.SaaS介绍与比较 随着云计算.大数据.人工智能等一批新技术的涌入,企业信息化建设速度加快,基于云计算的Iaa ...

  10. git submodule git 子模块管理相关操作

    Git 子模块操作相关的一些命令备忘: # 当使用git clone下来的工程中带有submodule时,初始的时候 submodule的内容并不会自动下载下来的,需执行如下命令: git submo ...