Spring AOP官方文档学习笔记(三)之基于xml的Spring AOP

打印 上一主题 下一主题

主题 1016|帖子 1016|积分 3048

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

x
1.声明schema,导入命名空间
(1)如果我们想要使用基于xml的spring aop,那么,第一步,我们需要在xml配置文件中声明spring aop schema,导入命名空间,如下这是一个标准的模板
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans>
复制代码
(2)在xml配置文件中,所有的切面以及通知等都必须放置于标签内
2.声明一个切面
  1. //定义一个切面类Logger,在其中声明一个前置通知public class Logger {    public void beforePrint() {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans>System.out.println("before...");    }}<?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="
  15.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  16.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.    
  19. </beans><?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xsi:schemaLocation="
  24.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  25.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.    
  28. </beans><?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="
  33.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  34.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.    
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="
  42.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  43.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  44.    
  45.    
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="
  51.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  52.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  53.    
  54.    
  55. </beans><?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xmlns:aop="http://www.springframework.org/schema/aop"
  59.        xsi:schemaLocation="
  60.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  61.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  62.    
  63.    
  64. </beans><?xml version="1.0" encoding="UTF-8"?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.        xmlns:aop="http://www.springframework.org/schema/aop"
  68.        xsi:schemaLocation="
  69.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  70.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  71.    
  72.    
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.        xmlns:aop="http://www.springframework.org/schema/aop"
  77.        xsi:schemaLocation="
  78.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  79.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  80.    
  81.    
  82. </beans>
复制代码
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:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans><?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="
  15.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  16.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.    
  19. </beans><?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xsi:schemaLocation="
  24.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  25.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.    
  28. </beans><?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="
  33.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  34.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.    
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="
  42.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  43.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  44.    
  45.    
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="
  51.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  52.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  53.    
  54.    
  55. </beans><?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xmlns:aop="http://www.springframework.org/schema/aop"
  59.        xsi:schemaLocation="
  60.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  61.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  62.    
  63.    
  64. </beans><?xml version="1.0" encoding="UTF-8"?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.        xmlns:aop="http://www.springframework.org/schema/aop"
  68.        xsi:schemaLocation="
  69.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  70.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  71.    
  72.    
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.        xmlns:aop="http://www.springframework.org/schema/aop"
  77.        xsi:schemaLocation="
  78.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  79.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  80.    
  81.    
  82. </beans><?xml version="1.0" encoding="UTF-8"?>
  83. <beans xmlns="http://www.springframework.org/schema/beans"
  84.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="
  87.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  88.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  89.    
  90.    
  91. </beans>
复制代码
4.声明一个通知
  1. //切面类public class Logger {    public void beforePrint() {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans>System.out.println("before...");    }    public void afterReturningPrint(Object returnVal) {<?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="
  15.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  16.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.    
  19. </beans>System.out.println(returnVal);<?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xsi:schemaLocation="
  24.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  25.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.    
  28. </beans>System.out.println("afterReturning...");    }    public void afterThrowingPrint(Throwable throwable) {<?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="
  33.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  34.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.    
  37. </beans>System.out.println(throwable);<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="
  42.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  43.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  44.    
  45.    
  46. </beans>System.out.println("afterThrowing...");    }    public void afterPrint() {<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="
  51.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  52.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  53.    
  54.    
  55. </beans>System.out.println("after...");    }    public void aroundPrint(ProceedingJoinPoint joinPoint) {<?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xmlns:aop="http://www.springframework.org/schema/aop"
  59.        xsi:schemaLocation="
  60.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  61.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  62.    
  63.    
  64. </beans>try {<?xml version="1.0" encoding="UTF-8"?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.        xmlns:aop="http://www.springframework.org/schema/aop"
  68.        xsi:schemaLocation="
  69.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  70.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  71.    
  72.    
  73. </beans>    System.out.println("before...");<?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.        xmlns:aop="http://www.springframework.org/schema/aop"
  77.        xsi:schemaLocation="
  78.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  79.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  80.    
  81.    
  82. </beans>    joinPoint.proceed();<?xml version="1.0" encoding="UTF-8"?>
  83. <beans xmlns="http://www.springframework.org/schema/beans"
  84.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="
  87.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  88.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  89.    
  90.    
  91. </beans>    System.out.println("after...");<?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xmlns:aop="http://www.springframework.org/schema/aop"
  95.        xsi:schemaLocation="
  96.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  97.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  98.    
  99.    
  100. </beans>} catch (Throwable throwable) {<?xml version="1.0" encoding="UTF-8"?>
  101. <beans xmlns="http://www.springframework.org/schema/beans"
  102.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  103.        xmlns:aop="http://www.springframework.org/schema/aop"
  104.        xsi:schemaLocation="
  105.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  106.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  107.    
  108.    
  109. </beans>    throwable.printStackTrace();<?xml version="1.0" encoding="UTF-8"?>
  110. <beans xmlns="http://www.springframework.org/schema/beans"
  111.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  112.        xmlns:aop="http://www.springframework.org/schema/aop"
  113.        xsi:schemaLocation="
  114.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  115.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  116.    
  117.    
  118. </beans>} finally {<?xml version="1.0" encoding="UTF-8"?>
  119. <beans xmlns="http://www.springframework.org/schema/beans"
  120.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  121.        xmlns:aop="http://www.springframework.org/schema/aop"
  122.        xsi:schemaLocation="
  123.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  124.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  125.    
  126.    
  127. </beans>    System.out.println("finally...");<?xml version="1.0" encoding="UTF-8"?>
  128. <beans xmlns="http://www.springframework.org/schema/beans"
  129.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.        xmlns:aop="http://www.springframework.org/schema/aop"
  131.        xsi:schemaLocation="
  132.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  133.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  134.    
  135.    
  136. </beans>}    }}<?xml version="1.0" encoding="UTF-8"?>
  137. <beans xmlns="http://www.springframework.org/schema/beans"
  138.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  139.        xmlns:aop="http://www.springframework.org/schema/aop"
  140.        xsi:schemaLocation="
  141.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  142.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  143.    
  144.    
  145. </beans><?xml version="1.0" encoding="UTF-8"?>
  146. <beans xmlns="http://www.springframework.org/schema/beans"
  147.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  148.        xmlns:aop="http://www.springframework.org/schema/aop"
  149.        xsi:schemaLocation="
  150.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  151.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  152.    
  153.    
  154. </beans><?xml version="1.0" encoding="UTF-8"?>
  155. <beans xmlns="http://www.springframework.org/schema/beans"
  156.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  157.        xmlns:aop="http://www.springframework.org/schema/aop"
  158.        xsi:schemaLocation="
  159.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  160.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  161.    
  162.    
  163. </beans><?xml version="1.0" encoding="UTF-8"?>
  164. <beans xmlns="http://www.springframework.org/schema/beans"
  165.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  166.        xmlns:aop="http://www.springframework.org/schema/aop"
  167.        xsi:schemaLocation="
  168.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  169.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  170.    
  171.    
  172. </beans><?xml version="1.0" encoding="UTF-8"?>
  173. <beans xmlns="http://www.springframework.org/schema/beans"
  174.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  175.        xmlns:aop="http://www.springframework.org/schema/aop"
  176.        xsi:schemaLocation="
  177.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  178.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  179.    
  180.    
  181. </beans><?xml version="1.0" encoding="UTF-8"?>
  182. <beans xmlns="http://www.springframework.org/schema/beans"
  183.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  184.        xmlns:aop="http://www.springframework.org/schema/aop"
  185.        xsi:schemaLocation="
  186.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  187.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  188.    
  189.    
  190. </beans><?xml version="1.0" encoding="UTF-8"?>
  191. <beans xmlns="http://www.springframework.org/schema/beans"
  192.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  193.        xmlns:aop="http://www.springframework.org/schema/aop"
  194.        xsi:schemaLocation="
  195.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  196.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  197.    
  198.    
  199. </beans><?xml version="1.0" encoding="UTF-8"?>
  200. <beans xmlns="http://www.springframework.org/schema/beans"
  201.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  202.        xmlns:aop="http://www.springframework.org/schema/aop"
  203.        xsi:schemaLocation="
  204.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  205.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  206.    
  207.    
  208. </beans><?xml version="1.0" encoding="UTF-8"?>
  209. <beans xmlns="http://www.springframework.org/schema/beans"
  210.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  211.        xmlns:aop="http://www.springframework.org/schema/aop"
  212.        xsi:schemaLocation="
  213.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  214.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  215.    
  216.    
  217. </beans><?xml version="1.0" encoding="UTF-8"?>
  218. <beans xmlns="http://www.springframework.org/schema/beans"
  219.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  220.        xmlns:aop="http://www.springframework.org/schema/aop"
  221.        xsi:schemaLocation="
  222.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  223.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  224.    
  225.    
  226. </beans><?xml version="1.0" encoding="UTF-8"?>
  227. <beans xmlns="http://www.springframework.org/schema/beans"
  228.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  229.        xmlns:aop="http://www.springframework.org/schema/aop"
  230.        xsi:schemaLocation="
  231.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  232.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  233.    
  234.    
  235. </beans>      
复制代码
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:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans><?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="
  15.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  16.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.    
  19. </beans><?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xsi:schemaLocation="
  24.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  25.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.    
  28. </beans><?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="
  33.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  34.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.    
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="
  42.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  43.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  44.    
  45.    
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="
  51.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  52.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  53.    
  54.    
  55. </beans><?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xmlns:aop="http://www.springframework.org/schema/aop"
  59.        xsi:schemaLocation="
  60.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  61.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  62.    
  63.    
  64. </beans><?xml version="1.0" encoding="UTF-8"?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.        xmlns:aop="http://www.springframework.org/schema/aop"
  68.        xsi:schemaLocation="
  69.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  70.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  71.    
  72.    
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.        xmlns:aop="http://www.springframework.org/schema/aop"
  77.        xsi:schemaLocation="
  78.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  79.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  80.    
  81.    
  82. </beans><?xml version="1.0" encoding="UTF-8"?>
  83. <beans xmlns="http://www.springframework.org/schema/beans"
  84.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="
  87.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  88.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  89.    
  90.    
  91. </beans>   
复制代码
6.声明一个引介
  1. public class ExampleA{}//希望向ExampleA中添加方法doSomething()public interface Extention {    void doSomething();}//doSomething()方法默认的实现public class ExtentionImpl implements Extention{    @Override    public void doSomething() {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans>System.out.println("doSomething...");    }}<?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="
  15.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  16.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.    
  19. </beans><?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xsi:schemaLocation="
  24.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  25.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.    
  28. </beans><?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="
  33.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  34.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.    
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="
  42.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  43.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  44.    
  45.    
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="
  51.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  52.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  53.    
  54.    
  55. </beans><?xml version="1.0" encoding="UTF-8"?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58.        xmlns:aop="http://www.springframework.org/schema/aop"
  59.        xsi:schemaLocation="
  60.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  61.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  62.    
  63.    
  64. </beans><?xml version="1.0" encoding="UTF-8"?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.        xmlns:aop="http://www.springframework.org/schema/aop"
  68.        xsi:schemaLocation="
  69.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  70.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  71.    
  72.    
  73. </beans>//使用引介,与基于注解的配置一致Extention exampleA = (Extention)ctx.getBean("exampleA");
复制代码
7.Advisors
(1) 除了使用标签外,我们还可以使用标签来声明一个切面,不过使用时,其所指向的bean必须要实现对应的Advice接口,如下
  1. //若要定义前置通知,则必须实现MethodBeforeAdvice接口,其他相应的通知也有对应的接口public class Logger implements MethodBeforeAdvice {    @Override    public void before(Method method, Object[] args, Object target) throws Throwable {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  8.    
  9.    
  10. </beans>System.out.println("advisor before...");    }}<?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xmlns:aop="http://www.springframework.org/schema/aop"
  14.        xsi:schemaLocation="
  15.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  16.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.    
  19. </beans><?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xmlns:aop="http://www.springframework.org/schema/aop"
  23.        xsi:schemaLocation="
  24.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  25.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  26.    
  27.    
  28. </beans><?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31.        xmlns:aop="http://www.springframework.org/schema/aop"
  32.        xsi:schemaLocation="
  33.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  34.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  35.    
  36.    
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.        xmlns:aop="http://www.springframework.org/schema/aop"
  41.        xsi:schemaLocation="
  42.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  43.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  44.    
  45.    
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.        xmlns:aop="http://www.springframework.org/schema/aop"
  50.        xsi:schemaLocation="
  51.         http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  52.         http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  53.    
  54.    
  55. </beans>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

盛世宏图

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表