• Spring之aop


    目录

    一,AOP的概念(即面向切面编程)

        1,目标(Target)

        2,通知(Advice)

        3   代理(Proxy )

        4   切入点(Pointcut)

        5   适配器(Advisor)

        6   连接点(Joinpoint)

     二:主要通知

    1.前置通知

    2.后置通知

    3.环绕通知

    4.异常通知

    5.过滤通知


    一,AOP的概念(即面向切面编程)


        1,目标(Target)


                    被通知(被代理)的对象

        2,通知(Advice)


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

        3   代理(Proxy )


                    将通知应用到目标对象后创建的对象(代理=目标+通知),只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的

        4   切入点(Pointcut)


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

        5   适配器(Advisor)


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

        6   连接点(Joinpoint)


                    程序执行过程中明确的点,如方法的调用,或者异常的抛出.
     

     二:主要通知

    1.前置通知

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

    2.后置通知

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

    3.环绕通知

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

     

    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>

    4.异常通知

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

     

    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>

    5.过滤通知

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

  • 相关阅读:
    Mysql索引Hash和BTree的区别
    Python吴恩达深度学习作业15 -- YOLO原理及应用(自动驾驶——汽车检测)
    SpringBoot中如何集成ThymeLeaf呢?
    A-Level经济真题每期一练(54)
    Redis分布式锁实现Redisson 15问
    考研数据结构大题整合_组三(LZH组)
    Linux下创建和删除用户
    FCN学习笔记
    BuyVM 卢森堡 VPS 测评
    人血清白蛋白修饰绿原酸/诺氟沙星/沙拉沙星, HSA-CA/Nor/Sarafloxacin
  • 原文地址:https://blog.csdn.net/m0_67864917/article/details/126221982