spring6-署理模式和AOP [复制链接]
发表于 2026-2-9 14:44:46 | 显示全部楼层 |阅读模式
jdbcTemplate

jdbcTemplate是spring提供的一个jdbc模板类,是对jdbc的封装。
固然你也可以使用其他框架融入MyBatis、Hibernate。
GoF之署理模式

署理模式的作用


  • 当一个对象须要受到掩护的时间,可以使用署理对象去完成某个举动。
  • 须要给某个对象举行功能加强的时间,可以找一个署理举行加强。
  • A对象和B对象无法直接交互时,也可以使用署理模式来完成。
署理模式中的三个脚色:

  • 目的对象
  • 署理对象
  • 目的对象和署理对象的公共接口
假如使用署理模式的话,客户端步伐是无法察觉的,客户端在使用署理对象的时间就像在使用目的对象。
署理模式分为静态署理和动态署理。
静态署理

目的对象类:
  1. // 目标对象
  2. public class OrderServiceImpl implements OrderService{
  3.     @Override
  4.     public void generateOrder() {
  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:context="http://www.springframework.org/schema/context"
  9.        xmlns:aop="http://www.springframework.org/schema/aop"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  11.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  12.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  13.     <context:component-scan base-package="com.ali.service" />
  14.     <aop:aspectj-autoproxy proxy-target-/>
  15. </beans>System.out.println("生成订单");
  16.     }
  17.     @Override
  18.     public void modifyOrder() {
  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:context="http://www.springframework.org/schema/context"
  23.        xmlns:aop="http://www.springframework.org/schema/aop"
  24.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  25.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  26.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  27.     <context:component-scan base-package="com.ali.service" />
  28.     <aop:aspectj-autoproxy proxy-target-/>
  29. </beans>System.out.println("修改订单");
  30.     }
  31.     @Override
  32.     public void detailOrder() {
  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:context="http://www.springframework.org/schema/context"
  37.        xmlns:aop="http://www.springframework.org/schema/aop"
  38.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  39.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  40.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  41.     <context:component-scan base-package="com.ali.service" />
  42.     <aop:aspectj-autoproxy proxy-target-/>
  43. </beans>System.out.println("查看订单详情");
  44.     }
  45. }
复制代码
署理对象类:
  1. // 代理对象
  2. public class OrderServiceProxy implements OrderService{
  3.     // 代理对象中含有目标对象的引用
  4.     // 这里使用OrderService类型,因为他耦合度低
  5.     private OrderService orderService;
  6.     // 构造方法传入目标对象
  7.     public OrderServiceProxy(OrderService orderService) {
  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:context="http://www.springframework.org/schema/context"
  12.        xmlns:aop="http://www.springframework.org/schema/aop"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  14.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  15.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  16.     <context:component-scan base-package="com.ali.service" />
  17.     <aop:aspectj-autoproxy proxy-target-/>
  18. </beans>this.orderService = orderService;
  19.     }
  20.     @Override
  21.     public void generateOrder() {
  22. <?xml version="1.0" encoding="UTF-8"?>
  23. <beans xmlns="http://www.springframework.org/schema/beans"
  24.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.        xmlns:context="http://www.springframework.org/schema/context"
  26.        xmlns:aop="http://www.springframework.org/schema/aop"
  27.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  28.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  29.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  30.     <context:component-scan base-package="com.ali.service" />
  31.     <aop:aspectj-autoproxy proxy-target-/>
  32. </beans>// 功能增强:统计方法执行时间
  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:context="http://www.springframework.org/schema/context"
  37.        xmlns:aop="http://www.springframework.org/schema/aop"
  38.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  39.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  40.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  41.     <context:component-scan base-package="com.ali.service" />
  42.     <aop:aspectj-autoproxy proxy-target-/>
  43. </beans>long begin = System.currentTimeMillis();
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <beans xmlns="http://www.springframework.org/schema/beans"
  46.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47.        xmlns:context="http://www.springframework.org/schema/context"
  48.        xmlns:aop="http://www.springframework.org/schema/aop"
  49.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  50.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  51.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  52.     <context:component-scan base-package="com.ali.service" />
  53.     <aop:aspectj-autoproxy proxy-target-/>
  54. </beans>orderService.generateOrder();
  55. <?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xmlns:context="http://www.springframework.org/schema/context"
  59.        xmlns:aop="http://www.springframework.org/schema/aop"
  60.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  61.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  62.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  63.     <context:component-scan base-package="com.ali.service" />
  64.     <aop:aspectj-autoproxy proxy-target-/>
  65. </beans>long end = System.currentTimeMillis();
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.        xmlns:context="http://www.springframework.org/schema/context"
  70.        xmlns:aop="http://www.springframework.org/schema/aop"
  71.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  72.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  73.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  74.     <context:component-scan base-package="com.ali.service" />
  75.     <aop:aspectj-autoproxy proxy-target-/>
  76. </beans>System.out.println("生成订单耗时:" + (end - begin) + "ms");
  77.     }
  78.     @Override
  79.     public void modifyOrder() {
  80. <?xml version="1.0" encoding="UTF-8"?>
  81. <beans xmlns="http://www.springframework.org/schema/beans"
  82.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  83.        xmlns:context="http://www.springframework.org/schema/context"
  84.        xmlns:aop="http://www.springframework.org/schema/aop"
  85.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  86.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  87.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  88.     <context:component-scan base-package="com.ali.service" />
  89.     <aop:aspectj-autoproxy proxy-target-/>
  90. </beans>long begin = System.currentTimeMillis();
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xmlns:context="http://www.springframework.org/schema/context"
  95.        xmlns:aop="http://www.springframework.org/schema/aop"
  96.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  97.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  98.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  99.     <context:component-scan base-package="com.ali.service" />
  100.     <aop:aspectj-autoproxy proxy-target-/>
  101. </beans>orderService.modifyOrder();
  102. <?xml version="1.0" encoding="UTF-8"?>
  103. <beans xmlns="http://www.springframework.org/schema/beans"
  104.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  105.        xmlns:context="http://www.springframework.org/schema/context"
  106.        xmlns:aop="http://www.springframework.org/schema/aop"
  107.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  108.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  109.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  110.     <context:component-scan base-package="com.ali.service" />
  111.     <aop:aspectj-autoproxy proxy-target-/>
  112. </beans>long end = System.currentTimeMillis();
  113. <?xml version="1.0" encoding="UTF-8"?>
  114. <beans xmlns="http://www.springframework.org/schema/beans"
  115.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.        xmlns:context="http://www.springframework.org/schema/context"
  117.        xmlns:aop="http://www.springframework.org/schema/aop"
  118.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  119.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  120.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  121.     <context:component-scan base-package="com.ali.service" />
  122.     <aop:aspectj-autoproxy proxy-target-/>
  123. </beans>System.out.println("修改订单耗时:" + (end - begin) + "ms");
  124.     }
  125.     @Override
  126.     public void detailOrder() {
  127. <?xml version="1.0" encoding="UTF-8"?>
  128. <beans xmlns="http://www.springframework.org/schema/beans"
  129.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.        xmlns:context="http://www.springframework.org/schema/context"
  131.        xmlns:aop="http://www.springframework.org/schema/aop"
  132.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  133.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  134.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  135.     <context:component-scan base-package="com.ali.service" />
  136.     <aop:aspectj-autoproxy proxy-target-/>
  137. </beans>long begin = System.currentTimeMillis();
  138. <?xml version="1.0" encoding="UTF-8"?>
  139. <beans xmlns="http://www.springframework.org/schema/beans"
  140.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  141.        xmlns:context="http://www.springframework.org/schema/context"
  142.        xmlns:aop="http://www.springframework.org/schema/aop"
  143.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  144.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  145.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  146.     <context:component-scan base-package="com.ali.service" />
  147.     <aop:aspectj-autoproxy proxy-target-/>
  148. </beans>orderService.detailOrder();
  149. <?xml version="1.0" encoding="UTF-8"?>
  150. <beans xmlns="http://www.springframework.org/schema/beans"
  151.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  152.        xmlns:context="http://www.springframework.org/schema/context"
  153.        xmlns:aop="http://www.springframework.org/schema/aop"
  154.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  155.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  156.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  157.     <context:component-scan base-package="com.ali.service" />
  158.     <aop:aspectj-autoproxy proxy-target-/>
  159. </beans>long end = System.currentTimeMillis();
  160. <?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:context="http://www.springframework.org/schema/context"
  164.        xmlns:aop="http://www.springframework.org/schema/aop"
  165.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  166.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  167.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  168.     <context:component-scan base-package="com.ali.service" />
  169.     <aop:aspectj-autoproxy proxy-target-/>
  170. </beans>System.out.println("查看订单详情耗时:" + (end - begin) + "ms");
  171.     }
  172. }
复制代码
公共接口:
  1. // 订单服务接口
  2. // 目标对象和代理对象的公共接口
  3. public interface OrderService {
  4.     // 生成订单
  5.     void generateOrder();
  6.     // 修改订单
  7.     void modifyOrder();
  8.     // 查看订单详情
  9.     void detailOrder();
  10. }
复制代码
测试:
  1. // 实现目标对象方法执行时间的统计
  2. public static void main(String[] args) {
  3.     // 创建目标对象
  4.     OrderService orderService = new OrderServiceImpl();
  5.     // 创建代理对象,同时将目标对象传入代理对象中
  6.     OrderServiceProxy orderServiceProxy = new OrderServiceProxy(orderService);
  7.     // 通过代理对象调用目标对象的方法
  8.     orderServiceProxy.generateOrder();
  9.     orderServiceProxy.modifyOrder();
  10.     orderServiceProxy.detailOrder();
  11. }
复制代码
静态署理长处:1.办理了ocp题目  2.采取署理模式的has a。低落了耦合度。
静态署理的缺点:假设体系中有上千个接口,每个接口都须要写署理类,如许类的数量会急剧膨胀,不好维护。
那怎么办理类爆炸的题目呢?
采取动态署理。  动态署理还是署理模式,只不外是在内存中为我们动态的天生一个class字节码,这个字节码就是署理类。
动态署理

在步伐运行阶段,在内存中动态天生署理类,成为动态署理。目的是镌汰署理类的数量。
常见的动态署理技能有:JDK动态署理(只能署理接口)、CGLIB动态署理、Javassist动态署理。
JDK动态署理

公共接口 和目的对象类引用 上面的代码。Jdk动态署理不须要写署理类,jdk会在内存中自动天生署理类,因此直接在客户端代码里直接调用:
客户端代码:
  1. public class Client {
  2.     public void main() {
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.        xmlns:context="http://www.springframework.org/schema/context"
  7.        xmlns:aop="http://www.springframework.org/schema/aop"
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  10.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  11.     <context:component-scan base-package="com.ali.service" />
  12.     <aop:aspectj-autoproxy proxy-target-/>
  13. </beans>// 1. 创建目标对象
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <beans xmlns="http://www.springframework.org/schema/beans"
  16.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  17.        xmlns:context="http://www.springframework.org/schema/context"
  18.        xmlns:aop="http://www.springframework.org/schema/aop"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  20.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  21.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  22.     <context:component-scan base-package="com.ali.service" />
  23.     <aop:aspectj-autoproxy proxy-target-/>
  24. </beans>OrderService orderService = new OrderServiceImpl();
  25. <?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xmlns:context="http://www.springframework.org/schema/context"
  29.        xmlns:aop="http://www.springframework.org/schema/aop"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  31.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  32.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  33.     <context:component-scan base-package="com.ali.service" />
  34.     <aop:aspectj-autoproxy proxy-target-/>
  35. </beans>// 2. 创建InvocationHandler对象
  36. <?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  42.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  43.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  44.     <context:component-scan base-package="com.ali.service" />
  45.     <aop:aspectj-autoproxy proxy-target-/>
  46. </beans>// 3. 创建代理对象
  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:context="http://www.springframework.org/schema/context"
  51.        xmlns:aop="http://www.springframework.org/schema/aop"
  52.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  53.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  54.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  55.     <context:component-scan base-package="com.ali.service" />
  56.     <aop:aspectj-autoproxy proxy-target-/>
  57. </beans>//    1. Proxy.newProxyInstance 的作用是创建代理对象。其实做了两件事:
  58. <?xml version="1.0" encoding="UTF-8"?>
  59. <beans xmlns="http://www.springframework.org/schema/beans"
  60.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  61.        xmlns:context="http://www.springframework.org/schema/context"
  62.        xmlns:aop="http://www.springframework.org/schema/aop"
  63.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  64.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  65.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  66.     <context:component-scan base-package="com.ali.service" />
  67.     <aop:aspectj-autoproxy proxy-target-/>
  68. </beans>//     1). 在内存中动态的构建一个类,
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xmlns:context="http://www.springframework.org/schema/context"
  73.        xmlns:aop="http://www.springframework.org/schema/aop"
  74.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  75.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  76.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  77.     <context:component-scan base-package="com.ali.service" />
  78.     <aop:aspectj-autoproxy proxy-target-/>
  79. </beans>//     2). 这个类要实现接口, 这个接口就是目标对象的接口,并且new了一个对象
  80. <?xml version="1.0" encoding="UTF-8"?>
  81. <beans xmlns="http://www.springframework.org/schema/beans"
  82.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  83.        xmlns:context="http://www.springframework.org/schema/context"
  84.        xmlns:aop="http://www.springframework.org/schema/aop"
  85.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  86.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  87.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  88.     <context:component-scan base-package="com.ali.service" />
  89.     <aop:aspectj-autoproxy proxy-target-/>
  90. </beans>//  2. 这个方法的三个参数:
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xmlns:context="http://www.springframework.org/schema/context"
  95.        xmlns:aop="http://www.springframework.org/schema/aop"
  96.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  97.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  98.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  99.     <context:component-scan base-package="com.ali.service" />
  100.     <aop:aspectj-autoproxy proxy-target-/>
  101. </beans>//    1). ClassLoader: 类加载器, 用于加载内存中的代理对象类. 和目标对象使用相同的类加载器
  102. <?xml version="1.0" encoding="UTF-8"?>
  103. <beans xmlns="http://www.springframework.org/schema/beans"
  104.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  105.        xmlns:context="http://www.springframework.org/schema/context"
  106.        xmlns:aop="http://www.springframework.org/schema/aop"
  107.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  108.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  109.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  110.     <context:component-scan base-package="com.ali.service" />
  111.     <aop:aspectj-autoproxy proxy-target-/>
  112. </beans>//   2). Class[]: 字节码数组, 代理对象和目标对象实现相同的接口. 用于让代理对象和目标对象具有相同的方法
  113. <?xml version="1.0" encoding="UTF-8"?>
  114. <beans xmlns="http://www.springframework.org/schema/beans"
  115.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.        xmlns:context="http://www.springframework.org/schema/context"
  117.        xmlns:aop="http://www.springframework.org/schema/aop"
  118.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  119.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  120.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  121.     <context:component-scan base-package="com.ali.service" />
  122.     <aop:aspectj-autoproxy proxy-target-/>
  123. </beans>//  3). InvocationHandler: 调用处理器对象,他是一个接口, 这个调用处理器用于编写增强代码
  124. <?xml version="1.0" encoding="UTF-8"?>
  125. <beans xmlns="http://www.springframework.org/schema/beans"
  126.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  127.        xmlns:context="http://www.springframework.org/schema/context"
  128.        xmlns:aop="http://www.springframework.org/schema/aop"
  129.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  130.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  131.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  132.     <context:component-scan base-package="com.ali.service" />
  133.     <aop:aspectj-autoproxy proxy-target-/>
  134. </beans>OrderService o = (OrderService)Proxy.newProxyInstance(OrderService.class.getClassLoader(),                         <?xml version="1.0" encoding="UTF-8"?>
  135. <beans xmlns="http://www.springframework.org/schema/beans"
  136.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  137.        xmlns:context="http://www.springframework.org/schema/context"
  138.        xmlns:aop="http://www.springframework.org/schema/aop"
  139.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  140.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  141.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  142.     <context:component-scan base-package="com.ali.service" />
  143.     <aop:aspectj-autoproxy proxy-target-/>
  144. </beans><?xml version="1.0" encoding="UTF-8"?>
  145. <beans xmlns="http://www.springframework.org/schema/beans"
  146.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  147.        xmlns:context="http://www.springframework.org/schema/context"
  148.        xmlns:aop="http://www.springframework.org/schema/aop"
  149.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  150.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  151.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  152.     <context:component-scan base-package="com.ali.service" />
  153.     <aop:aspectj-autoproxy proxy-target-/>
  154. </beans><?xml version="1.0" encoding="UTF-8"?>
  155. <beans xmlns="http://www.springframework.org/schema/beans"
  156.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  157.        xmlns:context="http://www.springframework.org/schema/context"
  158.        xmlns:aop="http://www.springframework.org/schema/aop"
  159.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  160.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  161.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  162.     <context:component-scan base-package="com.ali.service" />
  163.     <aop:aspectj-autoproxy proxy-target-/>
  164. </beans><?xml version="1.0" encoding="UTF-8"?>
  165. <beans xmlns="http://www.springframework.org/schema/beans"
  166.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  167.        xmlns:context="http://www.springframework.org/schema/context"
  168.        xmlns:aop="http://www.springframework.org/schema/aop"
  169.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  170.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  171.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  172.     <context:component-scan base-package="com.ali.service" />
  173.     <aop:aspectj-autoproxy proxy-target-/>
  174. </beans><?xml version="1.0" encoding="UTF-8"?>
  175. <beans xmlns="http://www.springframework.org/schema/beans"
  176.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  177.        xmlns:context="http://www.springframework.org/schema/context"
  178.        xmlns:aop="http://www.springframework.org/schema/aop"
  179.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  180.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  181.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  182.     <context:component-scan base-package="com.ali.service" />
  183.     <aop:aspectj-autoproxy proxy-target-/>
  184. </beans><?xml version="1.0" encoding="UTF-8"?>
  185. <beans xmlns="http://www.springframework.org/schema/beans"
  186.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  187.        xmlns:context="http://www.springframework.org/schema/context"
  188.        xmlns:aop="http://www.springframework.org/schema/aop"
  189.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  190.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  191.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  192.     <context:component-scan base-package="com.ali.service" />
  193.     <aop:aspectj-autoproxy proxy-target-/>
  194. </beans> orderService.getClass().getInterfaces(),
  195. <?xml version="1.0" encoding="UTF-8"?>
  196. <beans xmlns="http://www.springframework.org/schema/beans"
  197.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  198.        xmlns:context="http://www.springframework.org/schema/context"
  199.        xmlns:aop="http://www.springframework.org/schema/aop"
  200.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  201.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  202.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  203.     <context:component-scan base-package="com.ali.service" />
  204.     <aop:aspectj-autoproxy proxy-target-/>
  205. </beans><?xml version="1.0" encoding="UTF-8"?>
  206. <beans xmlns="http://www.springframework.org/schema/beans"
  207.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  208.        xmlns:context="http://www.springframework.org/schema/context"
  209.        xmlns:aop="http://www.springframework.org/schema/aop"
  210.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  211.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  212.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  213.     <context:component-scan base-package="com.ali.service" />
  214.     <aop:aspectj-autoproxy proxy-target-/>
  215. </beans><?xml version="1.0" encoding="UTF-8"?>
  216. <beans xmlns="http://www.springframework.org/schema/beans"
  217.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  218.        xmlns:context="http://www.springframework.org/schema/context"
  219.        xmlns:aop="http://www.springframework.org/schema/aop"
  220.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  221.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  222.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  223.     <context:component-scan base-package="com.ali.service" />
  224.     <aop:aspectj-autoproxy proxy-target-/>
  225. </beans>    new TimerInvocationHandler(orderService));
  226. <?xml version="1.0" encoding="UTF-8"?>
  227. <beans xmlns="http://www.springframework.org/schema/beans"
  228.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  229.        xmlns:context="http://www.springframework.org/schema/context"
  230.        xmlns:aop="http://www.springframework.org/schema/aop"
  231.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  232.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  233.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  234.     <context:component-scan base-package="com.ali.service" />
  235.     <aop:aspectj-autoproxy proxy-target-/>
  236. </beans>// 4. 通过代理对象调用方法
  237. <?xml version="1.0" encoding="UTF-8"?>
  238. <beans xmlns="http://www.springframework.org/schema/beans"
  239.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  240.        xmlns:context="http://www.springframework.org/schema/context"
  241.        xmlns:aop="http://www.springframework.org/schema/aop"
  242.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  243.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  244.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  245.     <context:component-scan base-package="com.ali.service" />
  246.     <aop:aspectj-autoproxy proxy-target-/>
  247. </beans>// 调用代理对象的代理方法时,如果代理方法的作用是功能增强,那目标对象的目标方法必须执行。
  248. <?xml version="1.0" encoding="UTF-8"?>
  249. <beans xmlns="http://www.springframework.org/schema/beans"
  250.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  251.        xmlns:context="http://www.springframework.org/schema/context"
  252.        xmlns:aop="http://www.springframework.org/schema/aop"
  253.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  254.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  255.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  256.     <context:component-scan base-package="com.ali.service" />
  257.     <aop:aspectj-autoproxy proxy-target-/>
  258. </beans>o.generateOrder();
  259.     }
  260. }
复制代码
调用处置惩罚器类:
  1. /**
  2. * 负责计时的一个调用处理器类
  3. * 在这个调用处理器中编写增强代码
  4. * 这个调用处理器只需要一个就好
  5. */
  6. public class TimerInvocationHandler implements InvocationHandler {
  7.     // 目标对象,就是要被增强的对象
  8.     private Object target;
  9.         // 构造方法传入目标对象
  10.     public TimerInvocationHandler(Object target) {
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xmlns:context="http://www.springframework.org/schema/context"
  15.        xmlns:aop="http://www.springframework.org/schema/aop"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  17.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  18.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  19.     <context:component-scan base-package="com.ali.service" />
  20.     <aop:aspectj-autoproxy proxy-target-/>
  21. </beans>    this.target = target;
  22.     }
  23.     // 这个方法必须是invoke()方法,因为jdk在底层会调用这个方法
  24.     // 这个方法什么时候调用?
  25.     //    什么时候通过代理对象调用方法的时候就会调用这个invoke()方法
  26.     // 这个方法的参数:
  27.     //    proxy: 代理对象,就是通过Proxy.newProxyInstance()方法创建的代理对象
  28.     //    method: 目标对象的目标方法,这就是要执行的目标方法
  29.     //    args: 目标方法上的参数
  30.     @Override
  31.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <beans xmlns="http://www.springframework.org/schema/beans"
  34.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.        xmlns:context="http://www.springframework.org/schema/context"
  36.        xmlns:aop="http://www.springframework.org/schema/aop"
  37.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  38.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  39.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  40.     <context:component-scan base-package="com.ali.service" />
  41.     <aop:aspectj-autoproxy proxy-target-/>
  42. </beans>// 功能增强:
  43. <?xml version="1.0" encoding="UTF-8"?>
  44. <beans xmlns="http://www.springframework.org/schema/beans"
  45.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.        xmlns:context="http://www.springframework.org/schema/context"
  47.        xmlns:aop="http://www.springframework.org/schema/aop"
  48.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  49.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  50.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  51.     <context:component-scan base-package="com.ali.service" />
  52.     <aop:aspectj-autoproxy proxy-target-/>
  53. </beans>Long begin = System.currentTimeMillis();
  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:context="http://www.springframework.org/schema/context"
  58.        xmlns:aop="http://www.springframework.org/schema/aop"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  60.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  61.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  62.     <context:component-scan base-package="com.ali.service" />
  63.     <aop:aspectj-autoproxy proxy-target-/>
  64. </beans>Object invoke = method.invoke(target, args);
  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:context="http://www.springframework.org/schema/context"
  69.        xmlns:aop="http://www.springframework.org/schema/aop"
  70.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  71.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  72.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  73.     <context:component-scan base-package="com.ali.service" />
  74.     <aop:aspectj-autoproxy proxy-target-/>
  75. </beans>Long end = System.currentTimeMillis();
  76. <?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.        xmlns:context="http://www.springframework.org/schema/context"
  80.        xmlns:aop="http://www.springframework.org/schema/aop"
  81.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  82.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  83.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  84.     <context:component-scan base-package="com.ali.service" />
  85.     <aop:aspectj-autoproxy proxy-target-/>
  86. </beans>System.out.println("方法执行耗时:" + (end - begin) + "ms");
  87. <?xml version="1.0" encoding="UTF-8"?>
  88. <beans xmlns="http://www.springframework.org/schema/beans"
  89.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  90.        xmlns:context="http://www.springframework.org/schema/context"
  91.        xmlns:aop="http://www.springframework.org/schema/aop"
  92.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  93.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  94.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  95.     <context:component-scan base-package="com.ali.service" />
  96.     <aop:aspectj-autoproxy proxy-target-/>
  97. </beans>return invoke;
  98.     }
  99. }
复制代码
CGLIB动态署理

既可以署理接口,也可以署理类。底层采取继承的方式实现,因此被署理的目的类不能被final修饰
目的类:
  1. // 目标类
  2. public class UserService {
  3.     public boolean login(String username, String password) {
  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:context="http://www.springframework.org/schema/context"
  8.        xmlns:aop="http://www.springframework.org/schema/aop"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  11.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  12.     <context:component-scan base-package="com.ali.service" />
  13.     <aop:aspectj-autoproxy proxy-target-/>
  14. </beans>System.out.println("用户登录,用户名:" + username + ",密码:" + password);
  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:context="http://www.springframework.org/schema/context"
  19.        xmlns:aop="http://www.springframework.org/schema/aop"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  21.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  22.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  23.     <context:component-scan base-package="com.ali.service" />
  24.     <aop:aspectj-autoproxy proxy-target-/>
  25. </beans>return "admin".equals(username) && "123456".equals(password);
  26.     }
  27.     public void logout(String username) {
  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:context="http://www.springframework.org/schema/context"
  32.        xmlns:aop="http://www.springframework.org/schema/aop"
  33.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  34.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  35.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  36.     <context:component-scan base-package="com.ali.service" />
  37.     <aop:aspectj-autoproxy proxy-target-/>
  38. </beans>System.out.println("用户退出登录,用户名:" + username);
  39.     }
  40. }
复制代码
客户端代码:
  1. public static void main(String[] args) {
  2.     // 创建字节码增强对象
  3.     // 这个对象是cglib的核心对象,依靠它来生成代理类
  4.     Enhancer enhancer = new Enhancer();
  5.     // 设置父类,也就是目标类
  6.     enhancer.setSuperclass(UserService.class);
  7.     // 设置回调函数(等同于jdk动态代理的中的调用处理器)
  8.     // 在cglib中是实现方法拦截器MethodInterceptor接口
  9.     enhancer.setCallback(new TimerMethodInterceptor());
  10.     UserService userServiceProxy = (UserService)enhancer.create();
  11.     boolean login = userServiceProxy.login("admin", "123456");
  12.     System.out.println(login?"登录成功":"登录失败");
  13.     userServiceProxy.logout("admin");
  14. }
复制代码
加强代码写在方法拦截器内里
  1. public class TimerMethodInterceptor implements MethodInterceptor {
  2.     @Override
  3.     public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
  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:context="http://www.springframework.org/schema/context"
  8.        xmlns:aop="http://www.springframework.org/schema/aop"
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  11.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  12.     <context:component-scan base-package="com.ali.service" />
  13.     <aop:aspectj-autoproxy proxy-target-/>
  14. </beans>Long startTime = System.currentTimeMillis();
  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:context="http://www.springframework.org/schema/context"
  19.        xmlns:aop="http://www.springframework.org/schema/aop"
  20.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  21.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  22.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  23.     <context:component-scan base-package="com.ali.service" />
  24.     <aop:aspectj-autoproxy proxy-target-/>
  25. </beans>Object o1 = methodProxy.invokeSuper(o, objects);
  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:context="http://www.springframework.org/schema/context"
  30.        xmlns:aop="http://www.springframework.org/schema/aop"
  31.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  32.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  33.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  34.     <context:component-scan base-package="com.ali.service" />
  35.     <aop:aspectj-autoproxy proxy-target-/>
  36. </beans>Long endTime = System.currentTimeMillis();
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:context="http://www.springframework.org/schema/context"
  41.        xmlns:aop="http://www.springframework.org/schema/aop"
  42.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  43.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  44.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  45.     <context:component-scan base-package="com.ali.service" />
  46.     <aop:aspectj-autoproxy proxy-target-/>
  47. </beans>System.out.println("方法 " + method.getName() + " 执行耗时:" + (endTime - startTime) + "ms");
  48. <?xml version="1.0" encoding="UTF-8"?>
  49. <beans xmlns="http://www.springframework.org/schema/beans"
  50.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51.        xmlns:context="http://www.springframework.org/schema/context"
  52.        xmlns:aop="http://www.springframework.org/schema/aop"
  53.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  54.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  55.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  56.     <context:component-scan base-package="com.ali.service" />
  57.     <aop:aspectj-autoproxy proxy-target-/>
  58. </beans>return o1;
  59.     }
  60. }
复制代码
留意:在jdk17环境下,cglib动态署理功能启动时会报错,须要添加启动时参数设置:
vm 参数:--add-opens java.base/java.lang=ALL-UNNAMED
program 参数:--add-opens java.base/sun.net.util=ALL-UNNAMED

面向切面编程AOP

在一个体系中一样平常会有很多体系服务,如:日志日志,变乱管理、安全等。这些服务成为交织业务。
这些交织业务是通用的。
假如在每一个业务处置惩罚过程中,都掺杂这些交织业务代码会出现2个题目:

  • 交织业务代码在多个业务中反复出现。代码没有得到复用,修改这些代码会非常困难。
  • 开发职员无法专业核心业务代码,在编写核心业务代码时还要处置惩罚这些交织业务代码。
这就须要使用AOP来办理以上题目。

总之,AOP就是将与核心业务无关的代码独立的抽取出来。形成一个独立的组件,然后以横向交织的方式应用到业务流程当中的过程。
aop底层使用动态署理技能实现。
spring AOP 使用的时jdk动态署理+cglib动态署理。spring 在这2种动态署理种机动切换。假如是署理接口,则使用jdk动态署理,假如署理某个类,则使用cglib。固然也可以手动设置来逼迫使用cglib。
AOP的七大术语

毗连点Joinpoint

在步伐实行流程中,可以织入切面的位置。方法的实行前后,非常抛出之后等位置。
毗连点形貌的是位置。
切点Pointcut

在步伐实行流程中,真正织入切面的方法。(一个切点对应多个毗连点)
切点形貌的是方法。
关照Advice

关照又叫加强,就是具体你要织入的的代码
关照包罗:前置关照、后置关照、围绕关照、非常关照、终极关照。
关照形貌的是代码。
切面Aspect

切点+关照就是切面。
织入Weaving

把关照应用到目的对象上的过程。
署理对象Proxy

一个目的对象被织入关照后产生的新对象。
目的对象Target

被织入关照的对象。
  1. public void main(String[] args) {
  2.     try {
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.        xmlns:context="http://www.springframework.org/schema/context"
  7.        xmlns:aop="http://www.springframework.org/schema/aop"
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  10.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  11.     <context:component-scan base-package="com.ali.service" />
  12.     <aop:aspectj-autoproxy proxy-target-/>
  13. </beans>// Joint point  连接点
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <beans xmlns="http://www.springframework.org/schema/beans"
  16.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  17.        xmlns:context="http://www.springframework.org/schema/context"
  18.        xmlns:aop="http://www.springframework.org/schema/aop"
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  20.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  21.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  22.     <context:component-scan base-package="com.ali.service" />
  23.     <aop:aspectj-autoproxy proxy-target-/>
  24. </beans>do1(); // Pointcut 切点
  25. <?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.        xmlns:context="http://www.springframework.org/schema/context"
  29.        xmlns:aop="http://www.springframework.org/schema/aop"
  30.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  31.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  32.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  33.     <context:component-scan base-package="com.ali.service" />
  34.     <aop:aspectj-autoproxy proxy-target-/>
  35. </beans>// Joint point  连接点
  36. <?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  42.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  43.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  44.     <context:component-scan base-package="com.ali.service" />
  45.     <aop:aspectj-autoproxy proxy-target-/>
  46. </beans>do2();// Pointcut 切点
  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:context="http://www.springframework.org/schema/context"
  51.        xmlns:aop="http://www.springframework.org/schema/aop"
  52.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  53.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  54.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  55.     <context:component-scan base-package="com.ali.service" />
  56.     <aop:aspectj-autoproxy proxy-target-/>
  57. </beans>// Joint point  连接点
  58. <?xml version="1.0" encoding="UTF-8"?>
  59. <beans xmlns="http://www.springframework.org/schema/beans"
  60.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  61.        xmlns:context="http://www.springframework.org/schema/context"
  62.        xmlns:aop="http://www.springframework.org/schema/aop"
  63.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  64.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  65.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  66.     <context:component-scan base-package="com.ali.service" />
  67.     <aop:aspectj-autoproxy proxy-target-/>
  68. </beans>do3();// Pointcut 切点
  69. <?xml version="1.0" encoding="UTF-8"?>
  70. <beans xmlns="http://www.springframework.org/schema/beans"
  71.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72.        xmlns:context="http://www.springframework.org/schema/context"
  73.        xmlns:aop="http://www.springframework.org/schema/aop"
  74.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  75.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  76.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  77.     <context:component-scan base-package="com.ali.service" />
  78.     <aop:aspectj-autoproxy proxy-target-/>
  79. </beans>// Joint point  连接点
  80. <?xml version="1.0" encoding="UTF-8"?>
  81. <beans xmlns="http://www.springframework.org/schema/beans"
  82.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  83.        xmlns:context="http://www.springframework.org/schema/context"
  84.        xmlns:aop="http://www.springframework.org/schema/aop"
  85.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  86.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  87.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  88.     <context:component-scan base-package="com.ali.service" />
  89.     <aop:aspectj-autoproxy proxy-target-/>
  90. </beans>do4();// Pointcut 切点
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xmlns:context="http://www.springframework.org/schema/context"
  95.        xmlns:aop="http://www.springframework.org/schema/aop"
  96.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  97.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  98.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  99.     <context:component-scan base-package="com.ali.service" />
  100.     <aop:aspectj-autoproxy proxy-target-/>
  101. </beans>// Joint point  连接点
  102. <?xml version="1.0" encoding="UTF-8"?>
  103. <beans xmlns="http://www.springframework.org/schema/beans"
  104.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  105.        xmlns:context="http://www.springframework.org/schema/context"
  106.        xmlns:aop="http://www.springframework.org/schema/aop"
  107.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  108.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  109.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  110.     <context:component-scan base-package="com.ali.service" />
  111.     <aop:aspectj-autoproxy proxy-target-/>
  112. </beans>do5();// Pointcut 切点
  113. <?xml version="1.0" encoding="UTF-8"?>
  114. <beans xmlns="http://www.springframework.org/schema/beans"
  115.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.        xmlns:context="http://www.springframework.org/schema/context"
  117.        xmlns:aop="http://www.springframework.org/schema/aop"
  118.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  119.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  120.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  121.     <context:component-scan base-package="com.ali.service" />
  122.     <aop:aspectj-autoproxy proxy-target-/>
  123. </beans>// Joint point  连接点
  124.     } catch (Exception e) {
  125. <?xml version="1.0" encoding="UTF-8"?>
  126. <beans xmlns="http://www.springframework.org/schema/beans"
  127.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  128.        xmlns:context="http://www.springframework.org/schema/context"
  129.        xmlns:aop="http://www.springframework.org/schema/aop"
  130.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  131.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  132.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  133.     <context:component-scan base-package="com.ali.service" />
  134.     <aop:aspectj-autoproxy proxy-target-/>
  135. </beans>// Joint point  连接点
  136.     }
  137. }
复制代码
切点表达式

切点表达式用来界说关照(Advice)往哪些方法上切入
使用Spring的AOP

Spring对AOP的实现包罗三种方式:

  • Spring连合AspectJ框架实现的AOP,基于注解方式
  • Spring连合AspectJ框架实现的AOP,基于XML方式
  • spring自己实现的AOP,基于xml设置方式
常用的是前2种方式。
准备工作

​        引入依靠
  1. <dependency>
  2.     <groupId>org.springframework</groupId>
  3.     <artifactId>spring-context</artifactId>
  4.     <version>6.0.4</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.springframework</groupId>
  8.     <artifactId>spring-aspects</artifactId>
  9.     <version>6.0.4</version>
  10. </dependency>
复制代码
引入定名空间(context和aop)和设置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:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  9.     <context:component-scan base-package="com.ali.service" />
  10.     <aop:aspectj-autoproxy proxy-target-/>
  11. </beans>
复制代码
编写目的类
  1. // 目的类@Servicepublic class UserService {    // 目的方法    public void login() {<?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:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  9.     <context:component-scan base-package="com.ali.service" />
  10.     <aop:aspectj-autoproxy proxy-target-/>
  11. </beans>System.out.println("UserService login....");    }}
复制代码
编写切面类
  1. // 切面类,须要@Aspect 标注@Aspect@Component("logAspect")public class LogAspect {    // 切面 = 关照+切点    // 关照就是加强,就是具体要编写的加强代码    // 这里关照以方法的情势出现。    // @Before 标注的方法就是一个前置关照    @Before("execution(* com.ali.service.UserService.*(..))")    public void advice( ) {<?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:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  9.     <context:component-scan base-package="com.ali.service" />
  10.     <aop:aspectj-autoproxy proxy-target-/>
  11. </beans>System.out.println("这是一个前置关照,方法实行前实行....");    }<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xmlns:context="http://www.springframework.org/schema/context"
  15.        xmlns:aop="http://www.springframework.org/schema/aop"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  17.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  18.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  19.     <context:component-scan base-package="com.ali.service" />
  20.     <aop:aspectj-autoproxy proxy-target-/>
  21. </beans>// 围绕关照是最大的关照,在前置关照之前,在后置关照之后    @Around("execution(* com.ali.service.UserService.*(..))")    public void around(ProceedingJoinPoint joinPoint) throws Throwable {<?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:context="http://www.springframework.org/schema/context"
  25.        xmlns:aop="http://www.springframework.org/schema/aop"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  27.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  28.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  29.     <context:component-scan base-package="com.ali.service" />
  30.     <aop:aspectj-autoproxy proxy-target-/>
  31. </beans>System.out.println("around start");<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xmlns:context="http://www.springframework.org/schema/context"
  35.        xmlns:aop="http://www.springframework.org/schema/aop"
  36.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  37.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  38.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  39.     <context:component-scan base-package="com.ali.service" />
  40.     <aop:aspectj-autoproxy proxy-target-/>
  41. </beans>// 实行目的方法<?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:context="http://www.springframework.org/schema/context"
  45.        xmlns:aop="http://www.springframework.org/schema/aop"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  47.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  48.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  49.     <context:component-scan base-package="com.ali.service" />
  50.     <aop:aspectj-autoproxy proxy-target-/>
  51. </beans>joinPoint.proceed();<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xmlns:context="http://www.springframework.org/schema/context"
  55.        xmlns:aop="http://www.springframework.org/schema/aop"
  56.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  57.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  58.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
  59.     <context:component-scan base-package="com.ali.service" />
  60.     <aop:aspectj-autoproxy proxy-target-/>
  61. </beans>System.out.println("around end");    }}
复制代码
测试代码:
  1. @Test
  2. public void test() {
  3.     ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  4.     UserService userService = context.getBean("userService", UserService.class);
  5.     userService.login();
  6. }
复制代码
留意:当有多个切面类时,可以使用@Order()注解举行优先级排序,数字越小,优先级越高,就先实行。比如:@Order(2) 比@Order(3) 先实行。
在每个方法上都写一遍切点表达式很贫苦,可以界说一个通用的切点表达式,然后在方法上使用这个通用的表达式即可。
  1. // 定义通用的切点表达式,后续通知直接使用方法名来引用切点表达式
  2. // 切点就是一个表达式,定义了在哪些连接点上执行通知
  3. @Pointcut("execution(* com.ali.service.UserService.*(..))")
  4. public void commmonPointcut() {
  5.     // 这个方法只是一个标识,方法体不需要编写任何代码
  6. }
  7. @AfterReturning("commmonPointcut()")
  8. public void afterAdvice( ) {
  9.     System.out.println("这是一个前置通知,方法执行前执行....");
  10. }
复制代码
JoinPoint的使用

关照方法可以参加参数JoinPoint,这个是spring容器自动传入的。
  1. @Before("execution(* com.ali.service.UserService.*(..))")
  2. public void advice(JoinPoint joinPoint) {
  3.     System.out.println("这是一个前置通知,方法执行前执行....");
  4.     // 使用JoinPoint对象获取连接点的信息
  5.     // joinPoint.getSignature() 获取连接点的方法签名
  6.     // 通过方法签名可以获取到这个方法的具体信息
  7.     // 获取方法名
  8.     String methodName = joinPoint.getSignature().getName();
  9.     System.out.println("正在执行的方法是:" + methodName);
  10. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表