12、Spring之基于xml的AOP

打印 上一主题 下一主题

主题 900|帖子 900|积分 2700

阅读本文前,建议先阅读Spring之基于注解的AOP
12.1、环境搭建

创建名为spring_aop_xml的新module,过程参考9.1节
12.1.1、配置打包方式和依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  9.    
  10.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  11. </beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12. <?xml version="1.0" encoding="UTF-8"?>
  13. <beans xmlns="http://www.springframework.org/schema/beans"
  14.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15.        xmlns:context="http://www.springframework.org/schema/context"
  16.        xmlns:aop="http://www.springframework.org/schema/aop"
  17.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  18.    
  19.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  20. </beans> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  21.     <modelVersion>4.0.0</modelVersion>
  22.     <groupId>org.rain</groupId>
  23.     <artifactId>spring_aop_xml</artifactId>
  24.     <version>1.0-SNAPSHOT</version>
  25.     <packaging>jar</packaging>
  26.     <dependencies>
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  33.    
  34.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  35. </beans>
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  42.    
  43.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  44. </beans><dependency>
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.        xmlns:context="http://www.springframework.org/schema/context"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  51.    
  52.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  53. </beans>    <groupId>org.springframework</groupId>
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  60.    
  61.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  62. </beans>    <artifactId>spring-context</artifactId>
  63. <?xml version="1.0" encoding="UTF-8"?>
  64. <beans xmlns="http://www.springframework.org/schema/beans"
  65.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  66.        xmlns:context="http://www.springframework.org/schema/context"
  67.        xmlns:aop="http://www.springframework.org/schema/aop"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  69.    
  70.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  71. </beans>    <version>5.3.1</version>
  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:context="http://www.springframework.org/schema/context"
  76.        xmlns:aop="http://www.springframework.org/schema/aop"
  77.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  78.    
  79.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  80. </beans></dependency>
  81. <?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.        xmlns:context="http://www.springframework.org/schema/context"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  87.    
  88.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  89. </beans>
  90. <?xml version="1.0" encoding="UTF-8"?>
  91. <beans xmlns="http://www.springframework.org/schema/beans"
  92.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  93.        xmlns:context="http://www.springframework.org/schema/context"
  94.        xmlns:aop="http://www.springframework.org/schema/aop"
  95.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  96.    
  97.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  98. </beans><dependency>
  99. <?xml version="1.0" encoding="UTF-8"?>
  100. <beans xmlns="http://www.springframework.org/schema/beans"
  101.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.        xmlns:context="http://www.springframework.org/schema/context"
  103.        xmlns:aop="http://www.springframework.org/schema/aop"
  104.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  105.    
  106.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  107. </beans>    <groupId>org.springframework</groupId>
  108. <?xml version="1.0" encoding="UTF-8"?>
  109. <beans xmlns="http://www.springframework.org/schema/beans"
  110.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  111.        xmlns:context="http://www.springframework.org/schema/context"
  112.        xmlns:aop="http://www.springframework.org/schema/aop"
  113.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  114.    
  115.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  116. </beans>    <artifactId>spring-aspects</artifactId>
  117. <?xml version="1.0" encoding="UTF-8"?>
  118. <beans xmlns="http://www.springframework.org/schema/beans"
  119.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  120.        xmlns:context="http://www.springframework.org/schema/context"
  121.        xmlns:aop="http://www.springframework.org/schema/aop"
  122.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  123.    
  124.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  125. </beans>    <version>5.3.1</version>
  126. <?xml version="1.0" encoding="UTF-8"?>
  127. <beans xmlns="http://www.springframework.org/schema/beans"
  128.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  129.        xmlns:context="http://www.springframework.org/schema/context"
  130.        xmlns:aop="http://www.springframework.org/schema/aop"
  131.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  132.    
  133.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  134. </beans></dependency>
  135. <?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:context="http://www.springframework.org/schema/context"
  139.        xmlns:aop="http://www.springframework.org/schema/aop"
  140.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  141.    
  142.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  143. </beans>
  144. <?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  150.    
  151.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  152. </beans><dependency>
  153. <?xml version="1.0" encoding="UTF-8"?>
  154. <beans xmlns="http://www.springframework.org/schema/beans"
  155.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  156.        xmlns:context="http://www.springframework.org/schema/context"
  157.        xmlns:aop="http://www.springframework.org/schema/aop"
  158.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  159.    
  160.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  161. </beans>    <groupId>junit</groupId>
  162. <?xml version="1.0" encoding="UTF-8"?>
  163. <beans xmlns="http://www.springframework.org/schema/beans"
  164.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165.        xmlns:context="http://www.springframework.org/schema/context"
  166.        xmlns:aop="http://www.springframework.org/schema/aop"
  167.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  168.    
  169.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  170. </beans>    <artifactId>junit</artifactId>
  171. <?xml version="1.0" encoding="UTF-8"?>
  172. <beans xmlns="http://www.springframework.org/schema/beans"
  173.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  174.        xmlns:context="http://www.springframework.org/schema/context"
  175.        xmlns:aop="http://www.springframework.org/schema/aop"
  176.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  177.    
  178.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  179. </beans>    <version>4.12</version>
  180. <?xml version="1.0" encoding="UTF-8"?>
  181. <beans xmlns="http://www.springframework.org/schema/beans"
  182.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  183.        xmlns:context="http://www.springframework.org/schema/context"
  184.        xmlns:aop="http://www.springframework.org/schema/aop"
  185.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  186.    
  187.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  188. </beans>    <scope>test</scope>
  189. <?xml version="1.0" encoding="UTF-8"?>
  190. <beans xmlns="http://www.springframework.org/schema/beans"
  191.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  192.        xmlns:context="http://www.springframework.org/schema/context"
  193.        xmlns:aop="http://www.springframework.org/schema/aop"
  194.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  195.    
  196.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  197. </beans></dependency>
  198.     </dependencies>
  199. </project>
复制代码
12.1.2、创建Calculator接口及实现类

  1. package org.rain.spring.aop.xml;
  2. /**
  3. * @author liaojy
  4. * @date 2023/8/18 - 8:09
  5. */
  6. public interface Calculator {
  7.     int add(int i, int j);
  8.     int sub(int i, int j);
  9.     int mul(int i, int j);
  10.     int div(int i, int j);
  11. }
复制代码
  1. package org.rain.spring.aop.xml;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * @author liaojy
  5. * @date 2023/8/18 - 8:11
  6. */
  7. // @Component注解:保证这个目标类能够放入IOC容器
  8. @Component
  9. public class CalculatorImpl implements Calculator {
  10.     public int add(int i, int j) {
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  19. </beans>int result = i + j;
  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:context="http://www.springframework.org/schema/context"
  24.        xmlns:aop="http://www.springframework.org/schema/aop"
  25.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  28. </beans>System.out.println("方法内部 result = " + result);
  29. <?xml version="1.0" encoding="UTF-8"?>
  30. <beans xmlns="http://www.springframework.org/schema/beans"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xmlns:context="http://www.springframework.org/schema/context"
  33.        xmlns:aop="http://www.springframework.org/schema/aop"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  37. </beans>return result;
  38.     }
  39.     public int sub(int i, int j) {
  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:context="http://www.springframework.org/schema/context"
  44.        xmlns:aop="http://www.springframework.org/schema/aop"
  45.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  46.    
  47.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  48. </beans>int result = i - j;
  49. <?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.        xmlns:context="http://www.springframework.org/schema/context"
  53.        xmlns:aop="http://www.springframework.org/schema/aop"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  55.    
  56.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  57. </beans>System.out.println("方法内部 result = " + result);
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  64.    
  65.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  66. </beans>return result;
  67.     }
  68.     public int mul(int i, int j) {
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  75.    
  76.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  77. </beans>int result = i * j;
  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:context="http://www.springframework.org/schema/context"
  82.        xmlns:aop="http://www.springframework.org/schema/aop"
  83.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  84.    
  85.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  86. </beans>System.out.println("方法内部 result = " + result);
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  93.    
  94.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  95. </beans>return result;
  96.     }
  97.     public int div(int i, int j) {
  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:context="http://www.springframework.org/schema/context"
  102.        xmlns:aop="http://www.springframework.org/schema/aop"
  103.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  104.    
  105.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  106. </beans>int result = i / j;
  107. <?xml version="1.0" encoding="UTF-8"?>
  108. <beans xmlns="http://www.springframework.org/schema/beans"
  109.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  110.        xmlns:context="http://www.springframework.org/schema/context"
  111.        xmlns:aop="http://www.springframework.org/schema/aop"
  112.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  113.    
  114.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  115. </beans>System.out.println("方法内部 result = " + result);
  116. <?xml version="1.0" encoding="UTF-8"?>
  117. <beans xmlns="http://www.springframework.org/schema/beans"
  118.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.        xmlns:context="http://www.springframework.org/schema/context"
  120.        xmlns:aop="http://www.springframework.org/schema/aop"
  121.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  122.    
  123.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  124. </beans>return result;
  125.     }
  126. }
复制代码
12.1.3、创建切面类LoggerAspect

  1. package org.rain.spring.aop.xml;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * @author liaojy
  5. * @date 2023/8/18 - 8:15
  6. */
  7. // @Component注解:保证这个目标类能够放入IOC容器
  8. @Component
  9. public class LoggerAspect {
  10. }
复制代码
12.1.4、创建spring配置文件

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>
复制代码
12.2、前置通知的使用

12.2.1、基本示例

12.2.1.1、定义前置通知的功能

  1.     public void beforeMethod(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>System.out.println("LoggerAspect,前置通知");    }
复制代码
12.2.1.2、配置前置通知到切面

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans><?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans><?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans><?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans><?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  47.    
  48.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  49. </beans><?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.        xmlns:context="http://www.springframework.org/schema/context"
  53.        xmlns:aop="http://www.springframework.org/schema/aop"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  55.    
  56.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  57. </beans>
复制代码
12.2.1.3、测试使用效果


由控制台日志可知,切面类的前置通知(方法),通过切入点表达式,作用到了目标方法的连接点上
  1. package org.rain.spring.aop.test;import org.junit.Test;import org.rain.spring.aop.xml.Calculator;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liaojy * @date 2023/8/18 - 19:07 */public class AOPTest {    @Test    public void testAOPByXML(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>// 注意:这里不能直接获取目标对象来使用;因为使用了AOP之后,IOC容器中就只有对应目标对象的代理对象;<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>// 如果强行获取目标对象,则报错:NoSuchBeanDefinitionException<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>//Calculator calculator = ioc.getBean(CalculatorImpl.class);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  41. </beans>// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  47.    
  48.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  49. </beans>Calculator calculator = ioc.getBean(Calculator.class);<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.        xmlns:context="http://www.springframework.org/schema/context"
  53.        xmlns:aop="http://www.springframework.org/schema/aop"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  55.    
  56.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  57. </beans>// 只能通过代理对象来访问目标对象中的方法<?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:context="http://www.springframework.org/schema/context"
  61.        xmlns:aop="http://www.springframework.org/schema/aop"
  62.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  63.    
  64.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  65. </beans>calculator.div(1,1);    }}
复制代码
12.2.2、高级示例

12.2.2.1、改进前置通知的功能


该示例中(前置)通知方法引入了连接点参数,通过连接点参数,可以动态获取(切入点表达式)对应的目标方法的名称和参数列表
  1.     // joinPoint参数:可以获取(通过切入点表达式定位出的)连接点的相关信息    public void beforeMethod(JoinPoint joinPoint){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>// 获取连接点所对应目标方法的名称<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>String methodName = joinPoint.getSignature().getName();<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>// 获取连接点所对应目标方法的参数列表<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>Object[] args = joinPoint.getArgs();<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  41. </beans>System.out.println("LoggerAspect-->前置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));    }
复制代码
12.2.2.2、测试使用效果


12.3、切入点表达式的复用

切入点表达的详细语法,请参考11.3.2节
12.3.1、配置公共的切入点表达式

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans><?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>
复制代码
12.3.2、使用公共的切入点表达式

  1. [/code][size=5]12.4、其他通知的使用[/size]
  2. [size=4]12.4.1、后置通知[/size]
  3. [size=3]12.4.1.1、定义后置通知的功能[/size]
  4. [img]https://img2023.cnblogs.com/blog/2052479/202308/2052479-20230818174707243-1104193875.png[/img]
  5. [code]    public void afterMethod(JoinPoint joinPoint){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  11.    
  12.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  13. </beans>String methodName = joinPoint.getSignature().getName();<?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:context="http://www.springframework.org/schema/context"
  17.        xmlns:aop="http://www.springframework.org/schema/aop"
  18.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  19.    
  20.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  21. </beans>Object[] args = joinPoint.getArgs();<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  27.    
  28.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  29. </beans>System.out.println("LoggerAspect-->后置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));    }
复制代码
12.4.1.2、配置后置通知到切面

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans><?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans><?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>
复制代码
12.4.1.3、测试使用效果


由控制台日志可知,后置通知在目标对象方法的finally子句中执行(一般用于释放资源)
  1.     @Test    public void testAOPByXML(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>Calculator calculator = ioc.getBean(Calculator.class);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>// 只能通过代理对象来访问目标对象中的方法<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  41. </beans>calculator.div(1,0);    }
复制代码
12.4.2、返回通知

12.4.2.1、定义返回通知的功能

  1.     public void afterReturningMethod(JoinPoint joinPoint,Object result){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>String methodName = joinPoint.getSignature().getName();<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>Object[] args = joinPoint.getArgs();<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>System.out.println("LoggerAspect-->返回通知,方法名:"+methodName+",结果:"+ result);    }
复制代码
12.4.2.2、配置返回通知到切面

  1. [/code][size=3]12.4.2.3、测试使用效果[/size]
  2. [img]https://img2023.cnblogs.com/blog/2052479/202308/2052479-20230818180426669-697920630.png[/img]
  3. [indent]返回通知在目标对象方法的返回值之后执行
  4. [/indent][code]    @Test    public void testAOPByXML(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  10.    
  11.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  12. </beans>ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");<?xml version="1.0" encoding="UTF-8"?>
  13. <beans xmlns="http://www.springframework.org/schema/beans"
  14.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15.        xmlns:context="http://www.springframework.org/schema/context"
  16.        xmlns:aop="http://www.springframework.org/schema/aop"
  17.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  18.    
  19.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  20. </beans>// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象<?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:context="http://www.springframework.org/schema/context"
  24.        xmlns:aop="http://www.springframework.org/schema/aop"
  25.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  28. </beans>Calculator calculator = ioc.getBean(Calculator.class);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  34.    
  35.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  36. </beans>// 只能通过代理对象来访问目标对象中的方法<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  42.    
  43.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  44. </beans>calculator.div(1,1);    }
复制代码
12.4.3、异常通知

12.4.3.1、定义异常通知的功能

  1.     public void afterThrowingMethod(JoinPoint joinPoint,Exception ex){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>String methodName = joinPoint.getSignature().getName();<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>Object[] args = joinPoint.getArgs();<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>System.out.println("LoggerAspect-->异常通知,方法名:"+methodName+",异常:"+ ex);    }
复制代码
12.4.3.2、配置异常通知到切面

  1. [/code][size=3]12.4.3.3、测试使用效果[/size]
  2. [img]https://img2023.cnblogs.com/blog/2052479/202308/2052479-20230818181316015-2146504502.png[/img]
  3. [indent]由控制台日志可知,异常通知在目标对象方法的catch子句中执行
  4. [/indent][code]    @Test    public void testAOPByXML(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  10.    
  11.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  12. </beans>ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");<?xml version="1.0" encoding="UTF-8"?>
  13. <beans xmlns="http://www.springframework.org/schema/beans"
  14.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15.        xmlns:context="http://www.springframework.org/schema/context"
  16.        xmlns:aop="http://www.springframework.org/schema/aop"
  17.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  18.    
  19.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  20. </beans>// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象<?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:context="http://www.springframework.org/schema/context"
  24.        xmlns:aop="http://www.springframework.org/schema/aop"
  25.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  28. </beans>Calculator calculator = ioc.getBean(Calculator.class);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  34.    
  35.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  36. </beans>// 只能通过代理对象来访问目标对象中的方法<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  42.    
  43.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  44. </beans>calculator.div(1,0);    }
复制代码
12.5、环绕通知

12.5.1、定义环绕通知的功能


环绕通知和动态代理的形式,非常相似
  1.     /**     *  环绕通知(方法)使用的参数是ProceedingJoinPoint类型     *  环绕通知(方法)的返回值,必须和目标对象方法的返回值一致     */    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>String methodName = proceedingJoinPoint.getSignature().getName();<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>Object[] args = proceedingJoinPoint.getArgs();<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>Object result = null;<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>try {<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  41. </beans>    System.out.println("LoggerAspect-->环绕前置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  47.    
  48.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  49. </beans>    // 表示目标对象方法的执行<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.        xmlns:context="http://www.springframework.org/schema/context"
  53.        xmlns:aop="http://www.springframework.org/schema/aop"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  55.    
  56.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  57. </beans>    result = proceedingJoinPoint.proceed();<?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:context="http://www.springframework.org/schema/context"
  61.        xmlns:aop="http://www.springframework.org/schema/aop"
  62.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  63.    
  64.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  65. </beans>    System.out.println("LoggerAspect-->环绕返回通知,方法名:"+methodName+",结果:"+ result);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  71.    
  72.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  73. </beans>} catch (Throwable throwable) {<?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:context="http://www.springframework.org/schema/context"
  77.        xmlns:aop="http://www.springframework.org/schema/aop"
  78.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  79.    
  80.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  81. </beans>    throwable.printStackTrace();<?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.        xmlns:context="http://www.springframework.org/schema/context"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  87.    
  88.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  89. </beans>    System.out.println("LoggerAspect-->环绕异常通知,方法名:"+methodName+",异常:"+ throwable);<?xml version="1.0" encoding="UTF-8"?>
  90. <beans xmlns="http://www.springframework.org/schema/beans"
  91.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  92.        xmlns:context="http://www.springframework.org/schema/context"
  93.        xmlns:aop="http://www.springframework.org/schema/aop"
  94.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  95.    
  96.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  97. </beans>}finally {<?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.        xmlns:context="http://www.springframework.org/schema/context"
  101.        xmlns:aop="http://www.springframework.org/schema/aop"
  102.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  103.    
  104.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  105. </beans>    System.out.println("LoggerAspect-->环绕后置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));<?xml version="1.0" encoding="UTF-8"?>
  106. <beans xmlns="http://www.springframework.org/schema/beans"
  107.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  108.        xmlns:context="http://www.springframework.org/schema/context"
  109.        xmlns:aop="http://www.springframework.org/schema/aop"
  110.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  111.    
  112.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  113. </beans>}<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  119.    
  120.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  121. </beans>return result;    }
复制代码
12.5.2、配置环绕通知到切面

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans><?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans><?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>
复制代码
12.5.3、测试使用效果


注意:因为环绕通知包括了其他四种通知,所以一般要么配置其他四种通知,要么只配置环绕通知;
本示例为了展示效果才同时配置
  1.     @Test    public void testAOPByXML(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>Calculator calculator = ioc.getBean(Calculator.class);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>// 只能通过代理对象来访问目标对象中的方法<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  41. </beans>calculator.div(1,1);    }
复制代码
12.6、切面的优先级

12.6.1、创建其他切面类ValidateAspect

  1. package org.rain.spring.aop.xml;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * @author liaojy
  5. * @date 2023/8/20 - 23:59
  6. */
  7. // @Component注解:保证这个目标类能够放入IOC容器
  8. @Component
  9. public class ValidateAspect {
  10. }
复制代码
12.6.2、定义前置通知的功能

  1.     public void beforeMethod(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>System.out.println("ValidateAspect-->前置通知");    }
复制代码
12.6.3、配置前置通知到切面

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans><?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans><?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>   
复制代码
12.6.4、测试使用效果


由控制台日志可知,ValidateAspect切面的前置通知方法生效了,但执行顺序在LoggerAspect切面的前置通知方法的后面
  1.     @Test    public void testAOPByXML(){<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans>ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");<?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans>// 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象<?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans>Calculator calculator = ioc.getBean(Calculator.class);<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>// 只能通过代理对象来访问目标对象中的方法<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  39.    
  40.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  41. </beans>calculator.div(1,1);    }
复制代码
12.6.5、调整切面的优先级

  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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  9. </beans><?xml version="1.0" encoding="UTF-8"?>
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  15.    
  16.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  17. </beans><?xml version="1.0" encoding="UTF-8"?>
  18. <beans xmlns="http://www.springframework.org/schema/beans"
  19.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  20.        xmlns:context="http://www.springframework.org/schema/context"
  21.        xmlns:aop="http://www.springframework.org/schema/aop"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  23.    
  24.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  25. </beans><?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  31.    
  32.     <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan>
  33. </beans>   
复制代码
12.6.6、测试调整后的效果


由控制台日志可知,ValidateAspect切面的前置通知方法的执行顺序,在LoggerAspect切面的前置通知方法的前面
这是因为ValidateAspect切面的Order属性值已设为2023,要小于LoggerAspect切面所使用的默认值(Integer的最大值2147483647)

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

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

半亩花草

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

标签云

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