Spring_在XML中声明切面

人,最大的敌人是自己。

AOP配置元素

  在Spring的aop命名空间中,提供多个元素用来在XML中声明切面。

  1)<aop:advisor>:定义AOP通知器

  2)<aop:after>:定义AOP后置通知(不管被通知的方法是否执行成功)

  3)<aop:after-returning>:定义AOP返回通知

  4)<aop:after-throwing>:定义AOP异常通知

  5)<aop:around>:定义AOP环绕通知

  6)<aop:aspect>:定义一个切面

  7)<aop:aspectj-autoproxy>:启用@Aspect注解驱动的切面

  8)<aop:before>:定义AOP前置通知

  9)<aop:config>:顶层的AOP配置元素,大多数的<asop:*>元素必须在该元素内

  10)<aop:declare-parents>:以透明的方式为被通知的对象引入额外的接口

  11)<aop:pointcut>:定义一个切点

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc 
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"
> <bean id="audience" class="chapter4.practice1.Audience"/> <aop:config> <!-- 声明切面 --> <aop:aspect ref="audience"> <!-- 定义切点 --> <aop:pointcut expression="executionexecution(** chapter4.practice1.Performance.perform(..))"
         id
="performance"/> <!-- 定义前置通知 --> <aop:before pointcut-ref="performance" method="perform"/> <!-- 定义返回通知 --> <aop:after-returning pointcut-ref="performance" method="performReturn"/> <!-- 定义异常通知 --> <aop:after-throwing pointcut-ref="performance" method="performThrowing"/> <!-- 定义环绕通知 --> <aop:around pointcut-ref="performance" method="aroundPerformance"/> <!-- 定义一个存在参数的切点,为通知传参数 --> <aop:pointcut expression="executionexecution(** chapter4.practice1.Performance.play(String))
          and args(gameName)"
id="game"/> </aop:aspect> </aop:config> </beans>

  虽然基于注解的自动化配置要优于Java的配置,基于Java的配置要优于基于XML的配置,但是,如果你需要声明切面的通知类不能添加注解的时候(类来自于第三方),那么就必须采用XML配置。

猜你喜欢

转载自www.cnblogs.com/dandelZH/p/8955827.html