• spring02


    目录

    一、什么是Aop?

     二、Aop的关键名词介绍

    三、通过案例讲解

    3.1 前置通知

     3.2 后置通知

     3.3环绕通知

     3.4 异常通知

     3.5 过滤通知

    一、什么是Aop?

    AOP(Aspect Oriented Programming)称为面向切面编程,利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低提高程序的可重用性,同时提高了开发的效率

     二、Aop的关键名词介绍

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

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

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

    通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)

    注2:完成切面编程

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

                 例子:外科医生+护士

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

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

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

       

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

    三、通过案例讲解

    建一个com.cdl.aop.biz的包,里面建一个类

    BookBiz 

    1. package com.cdl.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. }

    再建一个com.cdl.aop.biz.impl的包,里面建一个类

    BookBizImpl 

    1. package com.cdl.aop.biz.impl;
    2. import com.cdl.aop.biz.BookBiz;
    3. import com.cdl.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. }

    再建一个com.cdl.aop.exception的包,里面建一个类

    PriceException 

    1. package com.cdl.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. }

    3.1 前置通知

    实现org.springframework.aop.MethodBeforeAdvice接口

            买书、评论前加系统日志

     建一个包com.cdl.aop.advice,在里面再建一个类

    MyMethodBeforeAdvice

    1. package com.cdl.aop.advice;
    2. import java.lang.reflect.Method;
    3. import java.util.Arrays;
    4. import org.springframework.aop.MethodBeforeAdvice;
    5. /**
    6. * 实现org.springframework.aop.MethodBeforeAdvice接口
    7. 买书、评论前加系统日志
    8. * @author Lenovo
    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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
    10. <bean class="com.cdl.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz1">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="cdl">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>足球value>
    18. <value>唱歌value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.cdl.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz1">property>
    24. <constructor-arg name="name" value="cdl">constructor-arg>
    25. <constructor-arg name="age" value="23">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球1value>
    29. <value>足球1value>
    30. <value>唱歌1value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    35. <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    36. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    37. <property name="target" ref="bookBiz">property>
    38. <property name="proxyInterfaces">
    39. <list>
    40. <value>com.cdl.aop.biz.BookBizvalue>
    41. list>
    42. property>
    43. <property name="interceptorNames">
    44. <list>
    45. <value>myBeforevalue>
    46. list>
    47. property>
    48. bean>
    49. beans>

    新建一个com.cdl.aop.test包 建一个Demo1的类

    1. package com.cdl.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.cdl.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. bean.buy("陈冬丽", "哈利波特", 20.7d);
    10. bean.comment("陈冬丽", "真好看");
    11. }
    12. }

    结果:

       实现买书、评论前加系统日志

    1. package com.cdl.aop.test;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.cdl.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("陈冬丽", "哈利波特", 20.7d);
    11. bean.comment("陈冬丽", "真好看");
    12. }
    13. }

     3.2 后置通知

    实现org.springframework.aop.AfterReturningAdvice接口

            买书返利

     MyAfterReturningAdvice

    1. package com.cdl.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 returnValue, Method method, Object[] args, Object target) throws Throwable {
    8. //目标对象的类名
    9. String clzName = target.getClass().getName();
    10. //当前调用的方法是
    11. String methodName = method.getName();
    12. //当前调用方法所传递的参数
    13. String arg = Arrays.toString(args);
    14. System.out.println("[买书返利系统日志:]"+clzName+"."+methodName+"被调用,传递的参数为:"+args+"目标对象方法返回值为:"+returnValue);
    15. }
    16. }
    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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
    10. <bean class="com.cdl.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz1">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="cdl">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>足球value>
    18. <value>唱歌value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.cdl.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz1">property>
    24. <constructor-arg name="name" value="cdl">constructor-arg>
    25. <constructor-arg name="age" value="23">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球1value>
    29. <value>足球1value>
    30. <value>唱歌1value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    35. <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    36. <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    37. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    38. <property name="target" ref="bookBiz">property>
    39. <property name="proxyInterfaces">
    40. <list>
    41. <value>com.cdl.aop.biz.BookBizvalue>
    42. list>
    43. property>
    44. <property name="interceptorNames">
    45. <list>
    46. <value>myBeforevalue>
    47. <value>myAftervalue>
    48. list>
    49. property>
    50. bean>
    51. beans>

    Demo1不变

     3.3环绕通知

    org.aopalliance.intercept.MethodInterceptor

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

     

    1. package com.cdl.aop.advice;
    2. import java.util.Arrays;
    3. import org.aopalliance.intercept.MethodInterceptor;
    4. import org.aopalliance.intercept.MethodInvocation;
    5. /**
    6. * 环绕通知=前置通知+后置通知
    7. * @author Lenovo
    8. *
    9. */
    10. public class MyMethodInterceptor implements MethodInterceptor{
    11. @Override
    12. public Object invoke(MethodInvocation invocation) throws Throwable {
    13. //目标对象的类名
    14. String clzName = invocation.getThis().getClass().getName();
    15. //当前调用的方法是
    16. String methodName = invocation.getMethod().getName();
    17. //当前调用方法所传递的参数
    18. String args = Arrays.toString(invocation.getArguments());
    19. //方法的返回值 执行目标方法 BookBiz
    20. Object rs = invocation.proceed();
    21. System.out.println("[环绕通知:]"+clzName+"."+methodName+"被调用,传递的参数为:"+rs);
    22. return rs;
    23. }
    24. }
    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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
    10. <bean class="com.cdl.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz1">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="cdl">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>足球value>
    18. <value>唱歌value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.cdl.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz1">property>
    24. <constructor-arg name="name" value="cdl">constructor-arg>
    25. <constructor-arg name="age" value="23">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球1value>
    29. <value>足球1value>
    30. <value>唱歌1value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    35. <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    36. <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    37. <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    38. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    39. <property name="target" ref="bookBiz">property>
    40. <property name="proxyInterfaces">
    41. <list>
    42. <value>com.cdl.aop.biz.BookBizvalue>
    43. list>
    44. property>
    45. <property name="interceptorNames">
    46. <list>
    47. <value>myBeforevalue>
    48. <value>myAftervalue>
    49. <value>myMethodvalue>
    50. list>
    51. property>
    52. bean>
    53. beans>

     3.4 异常通知

    org.springframework.aop.ThrowsAdvice

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

     MyThrowsAdvice 

    1. package com.cdl.aop.advice;
    2. import org.springframework.aop.ThrowsAdvice;
    3. import com.cdl.aop.exception.PriceException;
    4. /**
    5. * 关于异常通知
    6. * 相较于前置通知、后置通知、环绕通知有一个非常大的区别
    7. * 前面三大通知都需要实现其中的方法
    8. * 异常通知不需要,但是,它的方法名是固定的
    9. * @author Lenovo
    10. *
    11. */
    12. public class MyThrowsAdvice implements ThrowsAdvice{
    13. public void afterThrowing(PriceException ex) {
    14. System.out.println("【异常通知】:当价格发生异常,那么执行此处代码块!!!");
    15. }
    16. }
    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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
    10. <bean class="com.cdl.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz1">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="cdl">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>足球value>
    18. <value>唱歌value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.cdl.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz1">property>
    24. <constructor-arg name="name" value="cdl">constructor-arg>
    25. <constructor-arg name="age" value="23">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球1value>
    29. <value>足球1value>
    30. <value>唱歌1value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    35. <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    36. <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    37. <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    38. <bean class="com.cdl.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    39. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    40. <property name="target" ref="bookBiz">property>
    41. <property name="proxyInterfaces">
    42. <list>
    43. <value>com.cdl.aop.biz.BookBizvalue>
    44. list>
    45. property>
    46. <property name="interceptorNames">
    47. <list>
    48. <value>myBeforevalue>
    49. <value>myAftervalue>
    50. <value>myMethodvalue>
    51. <value>myThrowsvalue>
    52. list>
    53. property>
    54. bean>
    55. beans>

    将价格改成负数

     3.5 过滤通知

    org.springframework.aop.support.RegexpMethodPointcutAdvisor

            处理买书返利的bug

     

    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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
    10. <bean class="com.cdl.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz1">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="cdl">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>足球value>
    18. <value>唱歌value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.cdl.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz1">property>
    24. <constructor-arg name="name" value="cdl">constructor-arg>
    25. <constructor-arg name="age" value="23">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球1value>
    29. <value>足球1value>
    30. <value>唱歌1value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    35. <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    36. <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    37. <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    38. <bean class="com.cdl.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    39. <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myafterPlus">
    40. <property name="advice" ref="myAfter">property>
    41. <property name="pattern" value=".*buy">property>
    42. bean>
    43. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    44. <property name="target" ref="bookBiz">property>
    45. <property name="proxyInterfaces">
    46. <list>
    47. <value>com.cdl.aop.biz.BookBizvalue>
    48. list>
    49. property>
    50. <property name="interceptorNames">
    51. <list>
    52. <value>myBeforevalue>
    53. <value>myMethodvalue>
    54. <value>myThrowsvalue>
    55. <value>myafterPlusvalue>
    56. list>
    57. property>
    58. bean>
    59. beans>

    注意:将负数改回来

    结果

     

  • 相关阅读:
    【Pytorch with fastai】第 11 章 :使用 fastai 的中级 API 进行数据处理
    为什么五千块天价加急费都无法保证交期?
    编译原理实验--实验二 递归下降法判断算术表达式的正确性--Python实现
    刚爆火就下线的 ZAO 换脸,背后是另一场技术人的狂欢
    FastRCNN
    安装ipmitool时报错,EVP_CIPHER_CTX ctx 未识别
    微服务开发与实战Day11 - 微服务面试篇
    python笔记Ⅶ--函数返回值、作用域与命名空间、递归
    Effective C++条款21:必须返回对象时,别妄想返回其reference
    QT mysql 数据库线程池 与数据库操作封装
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126202971