• Spring-aop


    目录

    一、Aop所解决的问题

    1、aop简介

    2、作用

    二、Aop的关键名词介绍

    三、前置通知

    1、目标

    2、实现

    四、后置通知

    1、目标

    2、实现

    五、环绕通知

    1、目标

    2、实现

    六、异常通知

    1、目标

    2、实现

    七、过滤通知

    1、目标

    2、实现


    一、Aop所解决的问题

    1、aop简介

    AOP是一种面向切面编程,是一种编程思想,是面向对象编程(OOP)的一种补充。

    2、作用

    AOP 采取横向抽取机制(动态代理),取代了传统纵向继承机制的重复性代码,其应用主要体现在事务处理、日志管理、权限控制、异常处理等方面。

    主要作用是分离功能性需求和非功能性需求,使开发人员可以集中处理某一个关注点或者横切逻辑,减少对业务代码的侵入,增强代码的可读性和可维护性。

    简单的说,AOP 的作用就是保证开发者在不修改源代码的前提下,为系统中的业务组件添加某种通用功能。

    不需要在原有的业务逻辑中添加任何代码,可以实现共性·非业务代码

    二、Aop的关键名词介绍

    目标(Target)

            目标(Target):被通知(被代理)的对象

            注1:完成具体的业务逻辑

    连接点(Joinpoint)

            连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.

    通知(Advice)

            通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序        

            代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)

            注2:完成切面编程

    代理(Proxy)

            代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),

            例子:外科医生+护士

            注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的

    切入点(Pointcut)

            切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。

            (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)

    适配器(Advisor)

            适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)

    三、前置通知

    首先我们需要准备一些类

    BookBiz接口类

    1. package com.zking.aop.biz;
    2. public interface BookBiz {
    3. // 购书
    4. public boolean buy(String userName, String bookName, Double price);
    5. // 发表书评
    6. public void comment(String userName, String comments);
    7. }

    PriceException异常类

    1. package com.zking.aop.exception;
    2. public class PriceException extends RuntimeException {
    3. public PriceException() {
    4. super();
    5. }
    6. public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    7. super(message, cause, enableSuppression, writableStackTrace);
    8. }
    9. public PriceException(String message, Throwable cause) {
    10. super(message, cause);
    11. }
    12. public PriceException(String message) {
    13. super(message);
    14. }
    15. public PriceException(Throwable cause) {
    16. super(cause);
    17. }
    18. }

    BookBizImpl实现类

    1. package com.zking.aop.biz.impl;
    2. import com.zking.aop.biz.BookBiz;
    3. import com.zking.aop.exception.PriceException;
    4. public class BookBizImpl implements BookBiz {
    5. public BookBizImpl() {
    6. super();
    7. }
    8. public boolean buy(String userName, String bookName, Double price) {
    9. // 通过控制台的输出方式模拟购书
    10. if (null == price || price <= 0) {
    11. throw new PriceException("book price exception");
    12. }
    13. System.out.println(userName + " buy " + bookName + ", spend " + price);
    14. return true;
    15. }
    16. public void comment(String userName, String comments) {
    17. // 通过控制台的输出方式模拟发表书评
    18. System.out.println(userName + " say:" + comments);
    19. }
    20. }

    1、目标

    买书、评论前加系统日志

    2、实现

    前置通知类

    1. package com.zking.aop.advice;
    2. import java.lang.reflect.Method;
    3. import java.util.Arrays;
    4. import org.springframework.aop.MethodBeforeAdvice;
    5. /**
    6. * 前置通知
    7. * 买书、评论前加系统日志
    8. * @author Administrator
    9. *
    10. */
    11. public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
    12. @Override
    13. public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    14. //目标对象的类名
    15. String clzName = arg2.getClass().getName();
    16. //当前调用方法是
    17. String methodName = arg0.getName();
    18. //当前调用方法所传递的参数
    19. String args = Arrays.toString(arg1);
    20. System.out.println("【系统日志】:"+clzName+"."+methodName+"被调用,传递的参数为: "+args);
    21. }
    22. }

    配置Spring-context.xml文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    9. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    10. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    11. <property name="target" ref="bookBiz">property>
    12. <property name="proxyInterfaces" >
    13. <list>
    14. <value>com.zking.aop.biz.BookBizvalue>
    15. list>
    16. property>
    17. <property name="interceptorNames" >
    18. <list>
    19. <value>myBeforevalue>
    20. list>
    21. property>
    22. bean>
    23. beans>

    测试类

    1. package com.zking.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.zking.aop.biz.BookBiz;
    4. public class Demo1 {
    5. @SuppressWarnings("resource")
    6. public static void main(String[] args) {
    7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    8. // BookBiz bean = (BookBiz) context.getBean("bookBiz");
    9. BookBiz bean = (BookBiz) context.getBean("bookProxy");
    10. bean.buy("颠颠", "dpcq", 9.9d);
    11. bean.comment("颠颠", "三十年河东,三十年河西");
    12. }
    13. }

    运行结果

    四、后置通知

    1、目标

    实现买书返利(存在bug)

    2、实现

    后置通知类

    1. package com.zking.aop.advice;
    2. import java.lang.reflect.Method;
    3. import java.util.Arrays;
    4. import org.springframework.aop.AfterReturningAdvice;
    5. public class MyAfterReturningAdvice implements AfterReturningAdvice{
    6. @Override
    7. public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
    8. //目标对象的类名
    9. String clzName = arg3.getClass().getName();
    10. //当前调用方法是
    11. String methodName = arg1.getName();
    12. //当前调用方法所传递的参数
    13. String args = Arrays.toString(arg2);
    14. System.out.println("【买书返利】:"+clzName+"."+methodName+"被调用,传递的参数为: "+args+";目标对象方法返回值为:"+arg0);
    15. }
    16. }

    配置Spring-context.xml文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    9. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    10. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    11. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    12. <property name="target" ref="bookBiz">property>
    13. <property name="proxyInterfaces" >
    14. <list>
    15. <value>com.zking.aop.biz.BookBizvalue>
    16. list>
    17. property>
    18. <property name="interceptorNames" >
    19. <list>
    20. <value>myBeforevalue>
    21. <value>myAftervalue>
    22. list>
    23. property>
    24. bean>
    25. beans>

    测试类

    1. package com.zking.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.zking.aop.biz.BookBiz;
    4. public class Demo1 {
    5. @SuppressWarnings("resource")
    6. public static void main(String[] args) {
    7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    8. // BookBiz bean = (BookBiz) context.getBean("bookBiz");
    9. BookBiz bean = (BookBiz) context.getBean("bookProxy");
    10. bean.buy("颠颠", "dpcq", 9.9d);
    11. bean.comment("颠颠", "三十年河东,三十年河西");
    12. }
    13. }

    运行结果

    五、环绕通知

    1、目标

    类似拦截器,会包括切入点,目标类前后都会执行代码。

    2、实现

    环绕通知类

    1. package com.zking.aop.advice;
    2. import java.lang.reflect.Method;
    3. import java.util.Arrays;
    4. import org.aopalliance.intercept.MethodInterceptor;
    5. import org.aopalliance.intercept.MethodInvocation;
    6. /**
    7. * 环绕通知=前置通知+后置通知
    8. * @author Administrator
    9. *
    10. */
    11. public class MyMethodInterceptor implements MethodInterceptor{
    12. @Override
    13. public Object invoke(MethodInvocation arg0) throws Throwable {
    14. //目标对象的类名
    15. String clzName = arg0.getThis().getClass().getName();
    16. //当前调用方法是
    17. String methodName = arg0.getMethod().getName();
    18. //当前调用方法所传递的参数
    19. String args = Arrays.toString(arg0.getArguments());
    20. //方法的返回值 执行目标方法 bookBiz.buy();
    21. Object rs = arg0.proceed();
    22. System.out.println("【环绕通知】:"+clzName+"."+methodName+"被调用,传递的参数为: "+args);
    23. System.out.println("【环绕通知】:目标对象方法返回值为:"+rs);
    24. return rs;
    25. }
    26. }

    配置Spring-context.xml文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    9. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    10. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    11. <bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    12. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    13. <property name="target" ref="bookBiz">property>
    14. <property name="proxyInterfaces" >
    15. <list>
    16. <value>com.zking.aop.biz.BookBizvalue>
    17. list>
    18. property>
    19. <property name="interceptorNames" >
    20. <list>
    21. <value>myBeforevalue>
    22. <value>myAftervalue>
    23. <value>myMethodvalue>
    24. list>
    25. property>
    26. bean>
    27. beans>

    测试类

    1. package com.zking.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.zking.aop.biz.BookBiz;
    4. public class Demo1 {
    5. @SuppressWarnings("resource")
    6. public static void main(String[] args) {
    7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    8. // BookBiz bean = (BookBiz) context.getBean("bookBiz");
    9. BookBiz bean = (BookBiz) context.getBean("bookProxy");
    10. bean.buy("颠颠", "dpcq", 9.9d);
    11. bean.comment("颠颠", "三十年河东,三十年河西");
    12. }
    13. }

    运行结果

     

    六、异常通知

    1、目标

    出现异常执行系统提示,然后进行处理。价格异常为例

    2、实现

    异常通知类

    1. package com.zking.aop.advice;
    2. import org.springframework.aop.ThrowsAdvice;
    3. import com.zking.aop.exception.PriceException;
    4. /**
    5. * 关于异常通知
    6. * 相较于前置通知、后置通知、环绕通知有一个非常大的区别
    7. * 前面三大通知都需要实现其中的方法
    8. * 异常通知则不需要,但是它的方法名是固定的
    9. * @author Administrator
    10. *
    11. */
    12. public class MyThrowsAdvice implements ThrowsAdvice{
    13. public void afterThrowing(PriceException p) {
    14. System.out.println("【异常通知】:当价格发生异常,那么执行此处代码");
    15. }
    16. }

    配置Spring-context.xml文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    9. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    10. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    11. <bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    12. <bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    13. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    14. <property name="target" ref="bookBiz">property>
    15. <property name="proxyInterfaces" >
    16. <list>
    17. <value>com.zking.aop.biz.BookBizvalue>
    18. list>
    19. property>
    20. <property name="interceptorNames" >
    21. <list>
    22. <value>myBeforevalue>
    23. <value>myAftervalue>
    24. <value>myMethodvalue>
    25. <value>myThrowsvalue>
    26. list>
    27. property>
    28. bean>
    29. beans>

    测试类

    1. package com.zking.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.zking.aop.biz.BookBiz;
    4. public class Demo1 {
    5. @SuppressWarnings("resource")
    6. public static void main(String[] args) {
    7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    8. // BookBiz bean = (BookBiz) context.getBean("bookBiz");
    9. BookBiz bean = (BookBiz) context.getBean("bookProxy");
    10. bean.buy("颠颠", "dpcq", -9.9d);
    11. bean.comment("颠颠", "三十年河东,三十年河西");
    12. }
    13. }

    运行效果

     

    七、过滤通知

    1、目标

    处理买书返利的bug

    注:过滤通知与其他的几大通知有所不一样,不用特别写一个通知类实现,

    而是Spring提供了一个类,我们只需要在spring-context.xml中配置就好了。

    2、实现

    配置Spring-context.xml文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    9. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    10. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    11. <bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    12. <bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    13. <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myafterPlus">
    14. <property name="advice" ref="myAfter">property>
    15. <property name="pattern" value=".*buy">property>
    16. bean>
    17. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    18. <property name="target" ref="bookBiz">property>
    19. <property name="proxyInterfaces" >
    20. <list>
    21. <value>com.zking.aop.biz.BookBizvalue>
    22. list>
    23. property>
    24. <property name="interceptorNames" >
    25. <list>
    26. <value>myBeforevalue>
    27. <value>myafterPlusvalue>
    28. <value>myMethodvalue>
    29. <value>myThrowsvalue>
    30. list>
    31. property>
    32. bean>
    33. beans>

    测试类

    1. package com.zking.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.zking.aop.biz.BookBiz;
    4. public class Demo1 {
    5. @SuppressWarnings("resource")
    6. public static void main(String[] args) {
    7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    8. // BookBiz bean = (BookBiz) context.getBean("bookBiz");
    9. BookBiz bean = (BookBiz) context.getBean("bookProxy");
    10. bean.buy("颠颠", "dpcq", 9.9d);
    11. bean.comment("颠颠", "三十年河东,三十年河西");
    12. }
    13. }

    运行结果

     

  • 相关阅读:
    数学分析:含参变量的积分
    使用Kubeadm安装简单集群实战
    【探索嵌入式虚拟化技术与应用】— 虚拟化技术深入浅出自学系列
    【Android知识笔记】Webview专题
    人工神经网络预测原理图,神经网络做预测的原理
    322. 零钱兑换
    Primavera P6 Professional 21.12 登录异常案例分享
    金三银四好像消失了,IT行业何时复苏!
    新际遇?不看必后悔,成都市人民政府培育大企业大集团的实施意见
    Leecode 15.题:三数之和
  • 原文地址:https://blog.csdn.net/yzq102873/article/details/126219197