• Spring之aop


    目录

    一、什么是aop

    1、aop简介

    2、作用

    3、AOP中关键性概念 

     4、 AOP带来的好处

    二、前置通知

    三、后置通知

     四、环绕通知

    五、异常通知

    六、过滤通知


    一、什么是aop


    1、aop简介

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

    2、作用

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

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

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

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

    3、AOP中关键性概念 

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

    2、目标(Target):被通知(被代理)的对象
    注1:完成具体的业务逻辑

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

    4、代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
                 例子:外科医生+护士
    注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的

    5、切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
                     (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
        
    适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)

     4、 AOP带来的好处

    让我们可以 “专心做事”
       案例:
       public void doSameBusiness (long lParam,String sParam){
         // 记录日志
         log.info("调用 doSameBusiness方法,参数是:"+lParam);
         // 输入合法性验证
         if (lParam<=0){
              throws new IllegalArgumentException("xx应该大于0");
         }
         if (sParam==null || sParam.trim().equals("")){
              throws new IllegalArgumentException("xx不能为空");
         }
         // 异常处理
         try{ ...
         }catch(...){
         }catch(...){
         }
         // 事务控制
         tx.commit();
       }

     

    二、前置通知

    ①spring-context.xml配置

    1. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    2. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    3. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    4. <property name="target" ref="bookBiz">property>
    5. <property name="proxyInterfaces">
    6. <list>
    7. <value>com.zking.aop.biz.BookBizvalue>
    8. list>
    9. property>
    10. <property name="interceptorNames">
    11. <list>
    12. <value>myBeforevalue>
    13. list>
    14. property>
    15. bean>

    ②MyMethodBeforeAdvice

    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. }

    ③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. }

    ④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. // logDao.add->sout("买书相关日志...")
    14. System.out.println(userName + " buy " + bookName + ", spend " + price);
    15. return true;
    16. }
    17. public void comment(String userName, String comments) {
    18. // 通过控制台的输出方式模拟发表书评
    19. System.out.println(userName + " say:" + comments);
    20. }
    21. }

    ⑤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. }

    ⑥Demo1

    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("lx", "jpm", 6.6d);
    11. bean.comment("sfd", "asd");
    12. }
    13. }

    三、后置通知

    ①spring-context.xml配置

    1. <beans default-autowire="byName" 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.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.zking.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="age" value="22">property>
    12. <property name="name" value="zhangsan">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>boyvalue>
    17. <value>篮球value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.zking.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. <constructor-arg name="name" value="zhangsan">constructor-arg>
    24. <constructor-arg name="age" value="22">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>boyvalue>
    29. <value>篮球value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    34. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    35. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">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.zking.aop.biz.BookBizvalue>
    41. list>
    42. property>
    43. <property name="interceptorNames">
    44. <list>
    45. <value>myBeforevalue>
    46. <value>myAftervalue>
    47. list>
    48. property>
    49. bean>
    50. beans>

    ②Demo1

    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("lx", "jpm", 6.6d);
    11. bean.comment("sfd", "asd");
    12. }
    13. }

    ③MyAfterReturningAdvice

    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. }

     四、环绕通知

    ①Demo1

    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("lx", "jpm", 6.6d);
    11. bean.comment("sfd", "asd");
    12. }
    13. }

    ②MethodInterceptor

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

    ③spring-context.xml

    1. <beans default-autowire="byName" 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.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.zking.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="age" value="22">property>
    12. <property name="name" value="zhangsan">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>boyvalue>
    17. <value>篮球value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.zking.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. <constructor-arg name="name" value="zhangsan">constructor-arg>
    24. <constructor-arg name="age" value="22">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>boyvalue>
    29. <value>篮球value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    34. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    35. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    36. <bean class="com.zking.aop.advice.MethodInterceptor" id="myMethod">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.zking.aop.biz.BookBizvalue>
    42. list>
    43. property>
    44. <property name="interceptorNames">
    45. <list>
    46. <value>myBeforevalue>
    47. <value>myAftervalue>
    48. <value>myMethodvalue>
    49. list>
    50. property>
    51. bean>
    52. beans>

    五、异常通知

    ①Demo1

    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("lx", "jpm", -6.6d);
    11. bean.comment("sfd", "asd");
    12. }
    13. }

    ②MyThrowsAdvice

    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 after(PriceException p) {
    14. System.out.println("【异常通知】:当价格发生异常,那么执行此处代码块!!!");
    15. }
    16. }

    ③spring-context.xml

    1. <beans default-autowire="byName" 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.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.zking.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="age" value="22">property>
    12. <property name="name" value="zhangsan">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>boyvalue>
    17. <value>篮球value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.zking.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. <constructor-arg name="name" value="zhangsan">constructor-arg>
    24. <constructor-arg name="age" value="22">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>boyvalue>
    29. <value>篮球value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    34. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    35. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    36. <bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    37. <bean class="com.zking.aop.advice.MethodInterceptor" 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.zking.aop.biz.BookBizvalue>
    43. list>
    44. property>
    45. <property name="interceptorNames">
    46. <list>
    47. <value>myBeforevalue>
    48. <value>myAftervalue>
    49. <value>myMethodvalue>
    50. <value>myThrowsvalue>
    51. list>
    52. property>
    53. bean>
    54. beans>

    六、过滤通知

    ①spring-context.xml

    1. <beans default-autowire="byName" 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.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.zking.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="age" value="22">property>
    12. <property name="name" value="zhangsan">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>boyvalue>
    17. <value>篮球value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.zking.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. <constructor-arg name="name" value="zhangsan">constructor-arg>
    24. <constructor-arg name="age" value="22">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>boyvalue>
    29. <value>篮球value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. <bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    34. <bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    35. <bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    36. <bean>bean>
    37. <bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    38. <bean class="com.zking.aop.advice.MethodInterceptor" id="myMethod">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.zking.aop.biz.BookBizvalue>
    48. list>
    49. property>
    50. <property name="interceptorNames">
    51. <list>
    52. <value>myBeforevalue>
    53. <value>myAfterPlusvalue>
    54. <value>myMethodvalue>
    55. <value>myThrowsvalue>
    56. list>
    57. property>
    58. bean>
    59. beans>

    ②Demo1

    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("lx", "jpm", 6.6d);
    11. bean.comment("sfd", "asd");
    12. }
    13. }

  • 相关阅读:
    继承语法详解
    抓住金三银四的尾巴,解锁程序员面试《刷题神器》
    《Go Web 编程》之第5章 内容展示
    Scratch软件编程等级考试二级——20200319
    R语言实现向量自回归和误差修正模型——附实战代码
    超大规模云数据中心对存储的诉求有哪些?
    bilibili杨宙:效能之上,高效交付
    虹科方案 | 虹科ATTO加速虚拟存储管理
    设计和实施
    VLAN技术 — Super VLAN
  • 原文地址:https://blog.csdn.net/qq_44247968/article/details/126223894