Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect
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/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://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>