?
??? Spring所有的切面和通知器都必須放在一個<aop:config>內(可以配置包含多個<aop:config>元素),每個<aop:config>包含pointcut,advisor和apsect元素。ps:他們必須按照這個順序進行聲明
- <aop:pointcut>:用來定義切入點,該切入點可以重用;
- <aop:advisor>:用來定義只有一個通知和一個切入點的切面;
- <aop:aspect>:用來定義切面,該切面可以包含多個切入點和通知,而且標簽內部的通知和切入點定義是無序的;和advisor的區別就在此,advisor只包含一個通知和一個切入點。
<aop:config> | AOP定義開始 |
?????? <aop:pointcut/> | ???? 切入點定義(0或多個) |
?????? <aop:advisor/> | ???? advisor定義(0或多個) |
?????? <aop:aspect> | ???? 切面定義開始(0或多個) |
??????????????? <aop:pointcut/> | ?????????? 切入點定義(0或多個) |
???????????????? <aop:before/> | ?????????? 前置通知(0或多個) |
???????????????? <aop:after-returning/> | ?????????? 返回后通知(0或多個) |
???????????????? <aop:after-throwing/> | ?????????? 拋出異常后通知(0或多個) |
???????????????? <aop:after/> | ?????????? 后通知(0或多個) |
???????????????? <aop:around/> | ?????????? 環繞通知(0或多個) |
????????????????? ? <aop:declare-parents/> | ?????????? 引入定義(0或多個) |
???????? <aop:aspect> | ???? 切面定義結束 |
</aop:config> | AOP定義結束 |
?
??? <aop:config>風格的配置大量使用了Spring的自動代理機制.
實現:
<aop:config><aop:aspect id="myAspect" ref="aBean">...</aop:aspect></aop:config><bean id="aBean" class="...">...</bean>
?
例子:
新建2個類
package com.aop.schema;
/*** * 切面類**/
public class MyAspect {}
?
package com.aop.schema;/*** * 業務類**/
public class ApsectBiz {}
?XML配置:
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><bean id="myAspect" class="com.aop.schema.MyAspect"></bean><bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean><aop:config><aop:aspect id="myAspectAOP" ref="myAspect"></aop:aspect></aop:config></beans>
?