1. 程式人生 > >Spring_在XML中聲明切面

Spring_在XML中聲明切面

execution ID pro -s declare spec microsoft 註解 lar

人,最大的敵人是自己。

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配置。

Spring_在XML中聲明切面