Spring的xml配置文件中的aop命名空间
一,Spring配置文件中aop命名空间
-
引入aop命名空间
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
二,aop命名空间常用标签及属性
-
<aop:config ></aop:config >
:配置织入,告诉spring,哪些方法(切点),需要哪些增强(前置通知,后置通知,环绕通知,异常通知,最终通知)<aop:config> </aop:config>
-
<aop:aspect ref=""></aop:aspect >
:声明切面,通过ref属性引入切面对象<aop:config> <aop:aspect></aop:aspect> </aop:config>
-
<aop:pointcut ><aop:pointcut >
:配置切点表达式,用于指定包下指定类的指定方法(指定返回数据类型,指定参数)得到增强<aop:config> <aop:aspect> <aop:pointcut id="myPointcut" expression="execution(* com.app.aop.*.*(..))"></aop:pointcut> </aop:aspect> </aop:config>
-
<aop:before method="切面对象中的前置增强方法名" pointcut-ref="切点引入" />
:切点方法执行前执行<aop:after-returning method="切面对象中的后置增强方法名" pointcut-ref="切点引入" />
:切点方法执行后执行<aop:around method="切面对象中的环绕增强方法名" pointcut-ref="切点引入" />
:切点方法执行前后都执行<aop:after-throwing method="切面对象中的前置增强方法名" pointcut-ref="切点引入" />
:切点方法出现异常执行<aop:after method="切面对象中的前置增强方法名" pointcut-ref="切点引入" />
:最终执行<aop:config> <aop:aspect> <aop:pointcut></aop:pointcut> <aop:before method="" pointcut-ref="" /> <aop:after-returning method="" pointcut-ref="" /> <aop:around method="" pointcut-ref="" /> <aop:after-throwing method="" pointcut-ref="" /> <aop:after method="" pointcut-ref="" /> </aop:aspect> </aop:config>
三,总结
- AOP面向切面编程,一个5钟类型的增强,前置,后置,环绕,异常,最终
- 切点表达式,用于指定包下的指定返回类型和参数的方法(切点)得到增强