1.实现前置增强 必须实现接口MethodBeforeAdvice接口

创建对应的文件

public interface Animal {//主业务接口void  eat();  //目标方法void  sleep();
}

Animal接口

public class Dog implements Animal {//目标对象/** 专心我们的逻辑  * 至于方法之前和方法之后要做的事情!我不关注!* Ioc:降低了主业务之间的耦合度* AOP:降低了主业务和交叉业务逻辑之间的耦合度*/@Overridepublic void eat() {System.out.println("Dog在吃骨头");}@Overridepublic void sleep() {System.out.println("Dog睡觉");}}

Dog实现类

public class AnimalBeforeAdvice implements MethodBeforeAdvice {/*** method:执行的方法* args:方法的参数* target:目标类对象*/@Overridepublic void before(Method method, Object[] args, Object target)throws Throwable {System.out.println("进入 了 前置 增强..............MethodBeforeAdvice");}}

前置增强类

<?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"  xmlns:c="http://www.springframework.org/schema/c"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
        http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/><!-- 配置前置 增强 -->
<bean id="before" class="cn.bdqn.advice.AnimalBeforeAdvice"/><!--  生成 代理对象  --><bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 设置目标对象(被代理的对象)<property name="targetName" value="dog"/>  --><property name="target" ref="dog"/><!-- 设置增强方式 --><property name="interceptorNames"><value>before</value></property></bean></beans>

applicationContext.xml文件

public class AnimalTest {@Testpublic  void  test01(){    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//使用代理对象进行操作Animal animal=(Animal) context.getBean("proxyFactoryBean");animal.eat();System.out.println("************");animal.sleep();}
}

测试类

2.实现后置增强 必须实现接口AfterReturningAdvice接口

public class AnimalAfterAdvice implements AfterReturningAdvice {
/*** returnValue:返回值 ,能获取返回值 但是如果对返回值进行了操作!没有意义!* afterReturning()就没有返回值*/@Overridepublic void afterReturning(Object returnValue, Method method,Object[] args, Object target) throws Throwable {System.out.println("执行了 后置 通知.................AfterReturningAdvice");}}

后置增强类

<?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"  xmlns:c="http://www.springframework.org/schema/c"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
        http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/><!-- 配置前置 通知-->
<bean id="before" class="cn.bdqn.advice.AnimalBeforeAdvice"/>
<!-- 配置后置 通知-->
<bean id="after" class="cn.bdqn.advice.AnimalAfterAdvice"/><!--  生成 代理对象  --><bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 设置目标对象(被代理的对象)<property name="targetName" value="dog"/>  --><property name="target" ref="dog"/><!-- 设置增强方式     同时配置多个增强方式01 .value="before,after"--><property name="interceptorNames" ><!--   02. 使用array<array><value>before</value><value>after</value></array> --><list><value>before</value><value>after</value></list></property></bean></beans>

修改后的xml文件

测试代码不变 直接运行

3.实现环绕增强 必须实现接口MethodInterceptor接口

public class AnimalAroundAdvice implements MethodInterceptor {
/*** * 环绕增强  是在前置增强之后  和后置增强之前  执行!* 可以对方法的返回值进行一系列的操作*/@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("方法之前........MethodInterceptor");Object result = invocation.proceed();//判断是否有返回值if (result!=null) {  result=((String)result).toUpperCase();}System.out.println("方法之后........MethodInterceptor");return result;}}

环绕增强类

可以修改Animal中的任意一个方法带有返回值的进行测试! 如

public interface Animal {//主业务接口String  eat();  //目标方法void  sleep();
}

改变后的Animal接口

public class Dog implements Animal {//目标对象
    @Overridepublic String eat() {System.out.println("Dog在吃骨头");return  "abcd";}@Overridepublic void sleep() {System.out.println("Dog睡觉");}}

改变后的Dog实现类

<?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"  xmlns:c="http://www.springframework.org/schema/c"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/>
<!-- 配置环绕 通知-->
<bean id="around" class="cn.bdqn.advice.AnimalAroundAdvice"/><!--  生成 代理对象  --><bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="dog"/><property name="interceptorNames" ><list><value>around</value></list></property></bean></beans>

xml文件

    //测试返回值
    @Testpublic  void  test02(){    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//使用代理对象进行操作Animal animal=(Animal) context.getBean("proxyFactoryBean");System.out.println(animal.eat());System.out.println("************");animal.sleep();}

测试类

效果图

4.实现自定义增强

并不是所有的方法都需要增强,那么我们就需要自定义增强的方法

<?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"  xmlns:c="http://www.springframework.org/schema/c"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 目标对象 -->
<bean id="dog" class="cn.bdqn.dao.impl.Dog"/><!-- 配置前置 通知-->
<bean id="before" class="cn.bdqn.advice.AnimalBeforeAdvice"/><!-- 自定义通知的切入点  --><bean id="myAdvice" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"><property name="advice"  ref="before"></property><property name="mappedNames"><list><value>eat</value> <!-- eat之外的方法就不会有前置通知了  --></list></property></bean><!--  生成 代理对象  --><bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="dog"/><property name="interceptorNames" ><list><value>myAdvice</value>  <!--引入自定义增强  --></list></property></bean></beans>

修改后的xml文件

测试代码不变

转载于:https://www.cnblogs.com/999-/p/6060875.html

spring06Aop相关推荐

最新文章

  1. [04] 前端构建工具区别
  2. 在eclipse中使用JDBC连接MySQL5.7.24
  3. 每天一道Java题[7]
  4. Ethermint部署及框架解析
  5. wdcp安装mysql_安装wdCP,无法连接mysql,解决方法
  6. Leetcode--826. 安排工作以达到最大收益
  7. 15947884 oracle_Oracle Patch Bundle Update
  8. OCF 试图为物联网建立标准,但它面临着不小的挑战
  9. 可穿戴设备的主流传感器介绍
  10. [git] gnutls_handshake() failed
  11. 计算机集成技术的研究和应用,信息系统集成技术研究
  12. Vue 中的 key 有什么作用?
  13. Mini USB针脚定义
  14. android 点赞飘心,点赞飘心动画组件FlyHeartView
  15. vTestStudio:变体Variant初理解
  16. word文档太大怎么压缩?快速压缩word文档
  17. Uva 437 巴比伦塔  UVA10003
  18. Desmos-可能是迄今为止最好用的免费Web端数学图像绘制工具
  19. Python的学习(十八)---- 单元测试工具nose
  20. 上传计算机桌面文件图标不见,关于桌面上图标都不见了这类问题的解决方法

热门文章

  1. 数据交互什么意思_学习编程怎么样才可以不枯燥?什么是前端语言?
  2. c语言单片机循环程序,单片机c语言教程:C51循环语句
  3. java常量映射_java-基础 - 常量与变量
  4. 第二章从收入的预测分析开始
  5. monkey自动化测试(命令行)
  6. 记在k8s的pod上使用apache的commons-net:ftp功能时,ftp一直上传文件失败
  7. 在mac os中设置环境变量
  8. 习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994, UVa232)
  9. diamond专题(一)– 简介和快速使用
  10. 《C语言及程序设计》实践参考——太乐了