java学习之spirng的aop

打印 上一主题 下一主题

主题 676|帖子 676|积分 2028

AOP技术

0x00前言

什么是AOP技术:在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
它的实际技术就是动态代理技术
0x01动态代理技术



相当于复习一下动态代理技术
  1. package auto_proxy;
  2. public interface tagerinterface {
  3.     public void save();
  4. }
  5. //写一个接口及代理对象
复制代码
  1. package auto_proxy;
  2. public class tager implements tagerinterface{
  3.     @Override
  4.     public void save() {
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <beans xmlns="http://www.springframework.org/schema/beans"
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8.        xmlns:aop="http://www.springframework.org/schema/aop"
  9.        xmlns:context="http://www.springframework.org/schema/context"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  11.        http://www.springframework.org/schema/beans/spring-beans.xsd
  12.        http://www.springframework.org/schema/aop
  13.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  14. >
  15.     <context:component-scan base-package="AOP_auto"/>
  16.     <aop:aspectj-autoproxy/>
  17. </beans>System.out.println("save running");
  18.     }
  19. }
  20. //写一个代理对象
复制代码
  1. package auto_proxy;
  2. public class Advice {
  3.     public void befor(){
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <beans xmlns="http://www.springframework.org/schema/beans"
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7.        xmlns:aop="http://www.springframework.org/schema/aop"
  8.        xmlns:context="http://www.springframework.org/schema/context"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  10.        http://www.springframework.org/schema/beans/spring-beans.xsd
  11.        http://www.springframework.org/schema/aop
  12.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  13. >
  14.     <context:component-scan base-package="AOP_auto"/>
  15.     <aop:aspectj-autoproxy/>
  16. </beans>System.out.println("前置增强");
  17.     }
  18.     public void after(){
  19. <?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xmlns:context="http://www.springframework.org/schema/context"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  25.        http://www.springframework.org/schema/beans/spring-beans.xsd
  26.        http://www.springframework.org/schema/aop
  27.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  28. >
  29.     <context:component-scan base-package="AOP_auto"/>
  30.     <aop:aspectj-autoproxy/>
  31. </beans>System.out.println("后置增强");
  32.     }
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <beans xmlns="http://www.springframework.org/schema/beans"
  35.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36.        xmlns:aop="http://www.springframework.org/schema/aop"
  37.        xmlns:context="http://www.springframework.org/schema/context"
  38.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  39.        http://www.springframework.org/schema/beans/spring-beans.xsd
  40.        http://www.springframework.org/schema/aop
  41.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  42. >
  43.     <context:component-scan base-package="AOP_auto"/>
  44.     <aop:aspectj-autoproxy/>
  45. </beans>}
  46. //这是增强方法
复制代码
  1. package auto_proxy;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. public class Proxytest {
  6.     public static void main(String[] args) {
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xmlns:aop="http://www.springframework.org/schema/aop"
  11.        xmlns:context="http://www.springframework.org/schema/context"
  12.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  13.        http://www.springframework.org/schema/beans/spring-beans.xsd
  14.        http://www.springframework.org/schema/aop
  15.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  16. >
  17.     <context:component-scan base-package="AOP_auto"/>
  18.     <aop:aspectj-autoproxy/>
  19. </beans>tager tager = new tager();
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xmlns:aop="http://www.springframework.org/schema/aop"
  24.        xmlns:context="http://www.springframework.org/schema/context"
  25.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  26.        http://www.springframework.org/schema/beans/spring-beans.xsd
  27.        http://www.springframework.org/schema/aop
  28.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  29. >
  30.     <context:component-scan base-package="AOP_auto"/>
  31.     <aop:aspectj-autoproxy/>
  32. </beans>Advice advice = new Advice();
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <beans xmlns="http://www.springframework.org/schema/beans"
  35.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36.        xmlns:aop="http://www.springframework.org/schema/aop"
  37.        xmlns:context="http://www.springframework.org/schema/context"
  38.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  39.        http://www.springframework.org/schema/beans/spring-beans.xsd
  40.        http://www.springframework.org/schema/aop
  41.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  42. >
  43.     <context:component-scan base-package="AOP_auto"/>
  44.     <aop:aspectj-autoproxy/>
  45. </beans>tagerinterface tagerinterface =(tagerinterface) Proxy.newProxyInstance(tager.getClass().getClassLoader(), tager.getClass().getInterfaces(), new InvocationHandler() {
  46. <?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xmlns:context="http://www.springframework.org/schema/context"
  51.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  52.        http://www.springframework.org/schema/beans/spring-beans.xsd
  53.        http://www.springframework.org/schema/aop
  54.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  55. >
  56.     <context:component-scan base-package="AOP_auto"/>
  57.     <aop:aspectj-autoproxy/>
  58. </beans>    @Override
  59. <?xml version="1.0" encoding="UTF-8"?>
  60. <beans xmlns="http://www.springframework.org/schema/beans"
  61.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  62.        xmlns:aop="http://www.springframework.org/schema/aop"
  63.        xmlns:context="http://www.springframework.org/schema/context"
  64.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  65.        http://www.springframework.org/schema/beans/spring-beans.xsd
  66.        http://www.springframework.org/schema/aop
  67.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  68. >
  69.     <context:component-scan base-package="AOP_auto"/>
  70.     <aop:aspectj-autoproxy/>
  71. </beans>    //调用代理对象的任何方法的时候都会执行invoke方法
  72. <?xml version="1.0" encoding="UTF-8"?>
  73. <beans xmlns="http://www.springframework.org/schema/beans"
  74.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  75.        xmlns:aop="http://www.springframework.org/schema/aop"
  76.        xmlns:context="http://www.springframework.org/schema/context"
  77.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.        http://www.springframework.org/schema/beans/spring-beans.xsd
  79.        http://www.springframework.org/schema/aop
  80.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  81. >
  82.     <context:component-scan base-package="AOP_auto"/>
  83.     <aop:aspectj-autoproxy/>
  84. </beans>    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  85. <?xml version="1.0" encoding="UTF-8"?>
  86. <beans xmlns="http://www.springframework.org/schema/beans"
  87.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.        xmlns:aop="http://www.springframework.org/schema/aop"
  89.        xmlns:context="http://www.springframework.org/schema/context"
  90.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  91.        http://www.springframework.org/schema/beans/spring-beans.xsd
  92.        http://www.springframework.org/schema/aop
  93.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  94. >
  95.     <context:component-scan base-package="AOP_auto"/>
  96.     <aop:aspectj-autoproxy/>
  97. </beans>       advice.befor();
  98. <?xml version="1.0" encoding="UTF-8"?>
  99. <beans xmlns="http://www.springframework.org/schema/beans"
  100.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  101.        xmlns:aop="http://www.springframework.org/schema/aop"
  102.        xmlns:context="http://www.springframework.org/schema/context"
  103.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  104.        http://www.springframework.org/schema/beans/spring-beans.xsd
  105.        http://www.springframework.org/schema/aop
  106.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  107. >
  108.     <context:component-scan base-package="AOP_auto"/>
  109.     <aop:aspectj-autoproxy/>
  110. </beans><?xml version="1.0" encoding="UTF-8"?>
  111. <beans xmlns="http://www.springframework.org/schema/beans"
  112.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  113.        xmlns:aop="http://www.springframework.org/schema/aop"
  114.        xmlns:context="http://www.springframework.org/schema/context"
  115.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  116.        http://www.springframework.org/schema/beans/spring-beans.xsd
  117.        http://www.springframework.org/schema/aop
  118.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  119. >
  120.     <context:component-scan base-package="AOP_auto"/>
  121.     <aop:aspectj-autoproxy/>
  122. </beans>method.invoke(tager, args);
  123. <?xml version="1.0" encoding="UTF-8"?>
  124. <beans xmlns="http://www.springframework.org/schema/beans"
  125.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  126.        xmlns:aop="http://www.springframework.org/schema/aop"
  127.        xmlns:context="http://www.springframework.org/schema/context"
  128.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  129.        http://www.springframework.org/schema/beans/spring-beans.xsd
  130.        http://www.springframework.org/schema/aop
  131.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  132. >
  133.     <context:component-scan base-package="AOP_auto"/>
  134.     <aop:aspectj-autoproxy/>
  135. </beans><?xml version="1.0" encoding="UTF-8"?>
  136. <beans xmlns="http://www.springframework.org/schema/beans"
  137.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  138.        xmlns:aop="http://www.springframework.org/schema/aop"
  139.        xmlns:context="http://www.springframework.org/schema/context"
  140.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  141.        http://www.springframework.org/schema/beans/spring-beans.xsd
  142.        http://www.springframework.org/schema/aop
  143.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  144. >
  145.     <context:component-scan base-package="AOP_auto"/>
  146.     <aop:aspectj-autoproxy/>
  147. </beans>advice.after();
  148. <?xml version="1.0" encoding="UTF-8"?>
  149. <beans xmlns="http://www.springframework.org/schema/beans"
  150.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151.        xmlns:aop="http://www.springframework.org/schema/aop"
  152.        xmlns:context="http://www.springframework.org/schema/context"
  153.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  154.        http://www.springframework.org/schema/beans/spring-beans.xsd
  155.        http://www.springframework.org/schema/aop
  156.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  157. >
  158.     <context:component-scan base-package="AOP_auto"/>
  159.     <aop:aspectj-autoproxy/>
  160. </beans><?xml version="1.0" encoding="UTF-8"?>
  161. <beans xmlns="http://www.springframework.org/schema/beans"
  162.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  163.        xmlns:aop="http://www.springframework.org/schema/aop"
  164.        xmlns:context="http://www.springframework.org/schema/context"
  165.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  166.        http://www.springframework.org/schema/beans/spring-beans.xsd
  167.        http://www.springframework.org/schema/aop
  168.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  169. >
  170.     <context:component-scan base-package="AOP_auto"/>
  171.     <aop:aspectj-autoproxy/>
  172. </beans>return null;
  173. <?xml version="1.0" encoding="UTF-8"?>
  174. <beans xmlns="http://www.springframework.org/schema/beans"
  175.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  176.        xmlns:aop="http://www.springframework.org/schema/aop"
  177.        xmlns:context="http://www.springframework.org/schema/context"
  178.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  179.        http://www.springframework.org/schema/beans/spring-beans.xsd
  180.        http://www.springframework.org/schema/aop
  181.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  182. >
  183.     <context:component-scan base-package="AOP_auto"/>
  184.     <aop:aspectj-autoproxy/>
  185. </beans>    }
  186. <?xml version="1.0" encoding="UTF-8"?>
  187. <beans xmlns="http://www.springframework.org/schema/beans"
  188.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  189.        xmlns:aop="http://www.springframework.org/schema/aop"
  190.        xmlns:context="http://www.springframework.org/schema/context"
  191.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  192.        http://www.springframework.org/schema/beans/spring-beans.xsd
  193.        http://www.springframework.org/schema/aop
  194.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  195. >
  196.     <context:component-scan base-package="AOP_auto"/>
  197.     <aop:aspectj-autoproxy/>
  198. </beans>});
  199. <?xml version="1.0" encoding="UTF-8"?>
  200. <beans xmlns="http://www.springframework.org/schema/beans"
  201.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  202.        xmlns:aop="http://www.springframework.org/schema/aop"
  203.        xmlns:context="http://www.springframework.org/schema/context"
  204.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  205.        http://www.springframework.org/schema/beans/spring-beans.xsd
  206.        http://www.springframework.org/schema/aop
  207.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  208. >
  209.     <context:component-scan base-package="AOP_auto"/>
  210.     <aop:aspectj-autoproxy/>
  211. </beans>//调用代理对象方法
  212. <?xml version="1.0" encoding="UTF-8"?>
  213. <beans xmlns="http://www.springframework.org/schema/beans"
  214.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  215.        xmlns:aop="http://www.springframework.org/schema/aop"
  216.        xmlns:context="http://www.springframework.org/schema/context"
  217.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  218.        http://www.springframework.org/schema/beans/spring-beans.xsd
  219.        http://www.springframework.org/schema/aop
  220.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  221. >
  222.     <context:component-scan base-package="AOP_auto"/>
  223.     <aop:aspectj-autoproxy/>
  224. </beans>tagerinterface.save();
  225.     }
  226. }
复制代码
0x02AOP

0x1AOP相关概念


  • Target(目标对象):代理对象的吧目标
  • Proxy(代理):一个类被AOP织 入增强以后,产生了一个结果代理类
  • Joinpoint(连接点):所谓连接点,指那些被拦截到的点(方法),可以被增强的方法
  • Pointcyt(切入点):所谓切入点我们要对那些地方进行拦截。(连接点是指可以加强的方法,切入点说的是真正被增强的)
0x2AOP入门案例

开发模式:xml和注解开发
思路:

  • 导入坐标
  • 制作连接点(原始操作,Dao接口,实现类)
  • 制作共性功能
  • 定义切入点
  • 绑定切入点与通知关系
还是用动态代理的方式
先写一个接口
  1. public interface tagerinterface {
  2.     void add();
  3.     void delete();
  4.     void update();
  5.     void qurey();
  6. }
复制代码
写一个实体函数去去继承接口
public class tager implements tagerinterface {
  1. @Override
  2. public void add() {
  3.     System.out.println("执行增加");
  4. }
  5. @Override
  6. public void delete() {
  7.     System.out.println("执行删除");
  8. }
  9. @Override
  10. public void update() {
  11.     System.out.println("执行更新");
  12. }
  13. @Override
  14. public void qurey() {
  15.     System.out.println("执行整理");
  16. }
  17. }
复制代码
写一个增强类去达到不改变原类去达到增强的效果
  1. public class MyAspect {
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xmlns:context="http://www.springframework.org/schema/context"
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.        http://www.springframework.org/schema/beans/spring-beans.xsd
  9.        http://www.springframework.org/schema/aop
  10.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  11. >
  12.     <context:component-scan base-package="AOP_auto"/>
  13.     <aop:aspectj-autoproxy/>
  14. </beans>public void before(){
  15. <?xml version="1.0" encoding="UTF-8"?>
  16. <beans xmlns="http://www.springframework.org/schema/beans"
  17.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.        xmlns:aop="http://www.springframework.org/schema/aop"
  19.        xmlns:context="http://www.springframework.org/schema/context"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  21.        http://www.springframework.org/schema/beans/spring-beans.xsd
  22.        http://www.springframework.org/schema/aop
  23.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  24. >
  25.     <context:component-scan base-package="AOP_auto"/>
  26.     <aop:aspectj-autoproxy/>
  27. </beans>    System.out.println("方法执行前执行");
  28. <?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xmlns:context="http://www.springframework.org/schema/context"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  34.        http://www.springframework.org/schema/beans/spring-beans.xsd
  35.        http://www.springframework.org/schema/aop
  36.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  37. >
  38.     <context:component-scan base-package="AOP_auto"/>
  39.     <aop:aspectj-autoproxy/>
  40. </beans>}
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.        xmlns:aop="http://www.springframework.org/schema/aop"
  45.        xmlns:context="http://www.springframework.org/schema/context"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  47.        http://www.springframework.org/schema/beans/spring-beans.xsd
  48.        http://www.springframework.org/schema/aop
  49.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  50. >
  51.     <context:component-scan base-package="AOP_auto"/>
  52.     <aop:aspectj-autoproxy/>
  53. </beans>public void after(){
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57.        xmlns:aop="http://www.springframework.org/schema/aop"
  58.        xmlns:context="http://www.springframework.org/schema/context"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  60.        http://www.springframework.org/schema/beans/spring-beans.xsd
  61.        http://www.springframework.org/schema/aop
  62.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  63. >
  64.     <context:component-scan base-package="AOP_auto"/>
  65.     <aop:aspectj-autoproxy/>
  66. </beans>    System.out.println("方法执行后执行");
  67. <?xml version="1.0" encoding="UTF-8"?>
  68. <beans xmlns="http://www.springframework.org/schema/beans"
  69.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  70.        xmlns:aop="http://www.springframework.org/schema/aop"
  71.        xmlns:context="http://www.springframework.org/schema/context"
  72.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  73.        http://www.springframework.org/schema/beans/spring-beans.xsd
  74.        http://www.springframework.org/schema/aop
  75.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  76. >
  77.     <context:component-scan base-package="AOP_auto"/>
  78.     <aop:aspectj-autoproxy/>
  79. </beans>}
  80. }
复制代码
写xml配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/aop
  8.        https://www.springframework.org/schema/aop/spring-aop.xsd">
  9.     <bean id="targer" />
  10.     <bean id="MyAspect" />
  11.    
  12.     <aop:config>
  13. <?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.        xmlns:aop="http://www.springframework.org/schema/aop"
  17.        xmlns:context="http://www.springframework.org/schema/context"
  18.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  19.        http://www.springframework.org/schema/beans/spring-beans.xsd
  20.        http://www.springframework.org/schema/aop
  21.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  22. >
  23.     <context:component-scan base-package="AOP_auto"/>
  24.     <aop:aspectj-autoproxy/>
  25. </beans>
  26. <?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.        xmlns:aop="http://www.springframework.org/schema/aop"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  32.        http://www.springframework.org/schema/beans/spring-beans.xsd
  33.        http://www.springframework.org/schema/aop
  34.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  35. >
  36.     <context:component-scan base-package="AOP_auto"/>
  37.     <aop:aspectj-autoproxy/>
  38. </beans><aop:aspect ref="MyAspect">
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  42.        xmlns:aop="http://www.springframework.org/schema/aop"
  43.        xmlns:context="http://www.springframework.org/schema/context"
  44.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  45.        http://www.springframework.org/schema/beans/spring-beans.xsd
  46.        http://www.springframework.org/schema/aop
  47.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  48. >
  49.     <context:component-scan base-package="AOP_auto"/>
  50.     <aop:aspectj-autoproxy/>
  51. </beans>   
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <beans xmlns="http://www.springframework.org/schema/beans"
  54.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  55.        xmlns:aop="http://www.springframework.org/schema/aop"
  56.        xmlns:context="http://www.springframework.org/schema/context"
  57.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  58.        http://www.springframework.org/schema/beans/spring-beans.xsd
  59.        http://www.springframework.org/schema/aop
  60.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  61. >
  62.     <context:component-scan base-package="AOP_auto"/>
  63.     <aop:aspectj-autoproxy/>
  64. </beans>    <aop:before method="before" pointcut="execution(public void AOP.tager.add())"/>
  65. <?xml version="1.0" encoding="UTF-8"?>
  66. <beans xmlns="http://www.springframework.org/schema/beans"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xmlns:aop="http://www.springframework.org/schema/aop"
  69.        xmlns:context="http://www.springframework.org/schema/context"
  70.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  71.        http://www.springframework.org/schema/beans/spring-beans.xsd
  72.        http://www.springframework.org/schema/aop
  73.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  74. >
  75.     <context:component-scan base-package="AOP_auto"/>
  76.     <aop:aspectj-autoproxy/>
  77. </beans>    <aop:before method="before" pointcut = "execution(public void AOP.tager.*(..))"/>
  78. <?xml version="1.0" encoding="UTF-8"?>
  79. <beans xmlns="http://www.springframework.org/schema/beans"
  80.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.        xmlns:aop="http://www.springframework.org/schema/aop"
  82.        xmlns:context="http://www.springframework.org/schema/context"
  83.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  84.        http://www.springframework.org/schema/beans/spring-beans.xsd
  85.        http://www.springframework.org/schema/aop
  86.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  87. >
  88.     <context:component-scan base-package="AOP_auto"/>
  89.     <aop:aspectj-autoproxy/>
  90. </beans></aop:aspect>
  91.     </aop:config>
  92. </beans>
复制代码

  • 可以使用通配符去通配整体方法
  • pointcut可以说是和你的切点,就是你要去增强那些方法
  • 增强的方法看aop标签有after before等等说
语法详解

  • 返回值类型、包名、类名、方法名、可以使用*****号代替任意
  • 包名和类名之间有一个.代表当前包下的类,两个个点代表当前包及其子包下面的类
  • 参数列表使用两个点代表任意个数
0x3通知类型

前置通知:在方法执行前通知
@Before(value = “”)
//环绕通知:可以将要执行的方法(point.proceed())进行包裹执行,可以在前后添加需要执行的操作
@Around(value = “”)
//后置通知:在方法正常执行完成进行通知,可以访问到方法的返回值的。
@AfterReturning(value = “”)
//异常通知:在方法出现异常时进行通知,可以访问到异常对象,且可以指定在出现特定异常时在执行通知。
@AfterThrowing(value = “”)
//方法执行后通知: 在目标方法执行后无论是否发生异常,执行通知,不能访问目标方法的执行的结果。
@After(value = “”)
0x03基于注解的开发
  1. package AOP_auto;
  2. import org.aspectj.lang.annotation.Aspect;
  3. import org.aspectj.lang.annotation.Before;
  4. import org.springframework.stereotype.Component;
  5. @Component("MyAspect")
  6. @Aspect//切面标志(增强方法)
  7. public class MyAspect {
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <beans xmlns="http://www.springframework.org/schema/beans"
  10.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.        xmlns:aop="http://www.springframework.org/schema/aop"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  14.        http://www.springframework.org/schema/beans/spring-beans.xsd
  15.        http://www.springframework.org/schema/aop
  16.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  17. >
  18.     <context:component-scan base-package="AOP_auto"/>
  19.     <aop:aspectj-autoproxy/>
  20. </beans>@Before("execution(public void AOP.tager.*(..))")
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xmlns:aop="http://www.springframework.org/schema/aop"
  25.        xmlns:context="http://www.springframework.org/schema/context"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  27.        http://www.springframework.org/schema/beans/spring-beans.xsd
  28.        http://www.springframework.org/schema/aop
  29.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  30. >
  31.     <context:component-scan base-package="AOP_auto"/>
  32.     <aop:aspectj-autoproxy/>
  33. </beans>public void before(){
  34. <?xml version="1.0" encoding="UTF-8"?>
  35. <beans xmlns="http://www.springframework.org/schema/beans"
  36.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  37.        xmlns:aop="http://www.springframework.org/schema/aop"
  38.        xmlns:context="http://www.springframework.org/schema/context"
  39.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  40.        http://www.springframework.org/schema/beans/spring-beans.xsd
  41.        http://www.springframework.org/schema/aop
  42.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  43. >
  44.     <context:component-scan base-package="AOP_auto"/>
  45.     <aop:aspectj-autoproxy/>
  46. </beans>    System.out.println("方法执行前执行");
  47. <?xml version="1.0" encoding="UTF-8"?>
  48. <beans xmlns="http://www.springframework.org/schema/beans"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xmlns:aop="http://www.springframework.org/schema/aop"
  51.        xmlns:context="http://www.springframework.org/schema/context"
  52.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  53.        http://www.springframework.org/schema/beans/spring-beans.xsd
  54.        http://www.springframework.org/schema/aop
  55.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  56. >
  57.     <context:component-scan base-package="AOP_auto"/>
  58.     <aop:aspectj-autoproxy/>
  59. </beans>}
  60. <?xml version="1.0" encoding="UTF-8"?>
  61. <beans xmlns="http://www.springframework.org/schema/beans"
  62.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  63.        xmlns:aop="http://www.springframework.org/schema/aop"
  64.        xmlns:context="http://www.springframework.org/schema/context"
  65.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.        http://www.springframework.org/schema/beans/spring-beans.xsd
  67.        http://www.springframework.org/schema/aop
  68.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  69. >
  70.     <context:component-scan base-package="AOP_auto"/>
  71.     <aop:aspectj-autoproxy/>
  72. </beans>public void after(){
  73. <?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.        xmlns:aop="http://www.springframework.org/schema/aop"
  77.        xmlns:context="http://www.springframework.org/schema/context"
  78.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  79.        http://www.springframework.org/schema/beans/spring-beans.xsd
  80.        http://www.springframework.org/schema/aop
  81.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  82. >
  83.     <context:component-scan base-package="AOP_auto"/>
  84.     <aop:aspectj-autoproxy/>
  85. </beans>    System.out.println("方法执行后执行");
  86. <?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.        xmlns:aop="http://www.springframework.org/schema/aop"
  90.        xmlns:context="http://www.springframework.org/schema/context"
  91.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  92.        http://www.springframework.org/schema/beans/spring-beans.xsd
  93.        http://www.springframework.org/schema/aop
  94.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  95. >
  96.     <context:component-scan base-package="AOP_auto"/>
  97.     <aop:aspectj-autoproxy/>
  98. </beans>}
  99. }
复制代码
  1. @Component("tager")
  2. public class tager implements tagerinterface {
  3.     @Override
  4.     @Autowired
  5.     public void add() {
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.        xmlns:aop="http://www.springframework.org/schema/aop"
  10.        xmlns:context="http://www.springframework.org/schema/context"
  11.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  12.        http://www.springframework.org/schema/beans/spring-beans.xsd
  13.        http://www.springframework.org/schema/aop
  14.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  15. >
  16.     <context:component-scan base-package="AOP_auto"/>
  17.     <aop:aspectj-autoproxy/>
  18. </beans>System.out.println("执行增加");
  19.     }
  20.     @Override
  21.     @Autowired
  22.     public void delete() {
  23. <?xml version="1.0" encoding="UTF-8"?>
  24. <beans xmlns="http://www.springframework.org/schema/beans"
  25.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  26.        xmlns:aop="http://www.springframework.org/schema/aop"
  27.        xmlns:context="http://www.springframework.org/schema/context"
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  29.        http://www.springframework.org/schema/beans/spring-beans.xsd
  30.        http://www.springframework.org/schema/aop
  31.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  32. >
  33.     <context:component-scan base-package="AOP_auto"/>
  34.     <aop:aspectj-autoproxy/>
  35. </beans>System.out.println("执行删除");
  36.     }
  37.     @Override
  38.     @Autowired
  39.     public void update() {
  40. <?xml version="1.0" encoding="UTF-8"?>
  41. <beans xmlns="http://www.springframework.org/schema/beans"
  42.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  43.        xmlns:aop="http://www.springframework.org/schema/aop"
  44.        xmlns:context="http://www.springframework.org/schema/context"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  46.        http://www.springframework.org/schema/beans/spring-beans.xsd
  47.        http://www.springframework.org/schema/aop
  48.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  49. >
  50.     <context:component-scan base-package="AOP_auto"/>
  51.     <aop:aspectj-autoproxy/>
  52. </beans>System.out.println("执行更新");
  53.     }
  54.     @Override
  55.     @Autowired
  56.     public void qurey() {
  57. <?xml version="1.0" encoding="UTF-8"?>
  58. <beans xmlns="http://www.springframework.org/schema/beans"
  59.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.        xmlns:aop="http://www.springframework.org/schema/aop"
  61.        xmlns:context="http://www.springframework.org/schema/context"
  62.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  63.        http://www.springframework.org/schema/beans/spring-beans.xsd
  64.        http://www.springframework.org/schema/aop
  65.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  66. >
  67.     <context:component-scan base-package="AOP_auto"/>
  68.     <aop:aspectj-autoproxy/>
  69. </beans>System.out.println("执行整理");
  70.     }
  71. }
复制代码
配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xmlns:context="http://www.springframework.org/schema/context"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.        http://www.springframework.org/schema/beans/spring-beans.xsd
  8.        http://www.springframework.org/schema/aop
  9.        https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
  10. >
  11.     <context:component-scan base-package="AOP_auto"/>
  12.     <aop:aspectj-autoproxy/>
  13. </beans>
复制代码
在xml文件中配置自动扫描和自动切面配置
0x04总结

AOP技术就一种动态代理技术的增强化,越学到后面发现一些原来不注意的注解和反射技术更重要。接下来会学习mvc了

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表