Spring 学习——Spring AOP——AOP配置篇Aspect、Pointcut
Schena——based AOP
- 声明
- Spring所有的切面和通知器都必须放在一个<aop:config>标签内,可以同时配置多个<aop:config>元素。
- 每一个<aop:config>内可以包含pointcut、advisor、aspect元素,但是必须按照这3个元素指定顺序进行声明。
声明切面Aspect
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean> <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"></aop:aspect> </aop:config> </beans>
- Next
声明通知Pointcut
- 使用形式
- com.jing.spring.service.AccountService.setAbnormal()。切入点为具体某一个方法。:Aspect和Spring Aop 都支持
- execution(public * *(..)) :切入点为执行所有public方法。:Aspect和Spring Aop 都支持
- execution(* set*(..)):切入点为执行所有set方法。:Aspect和Spring Aop 都支持
- execution(* com.jing.spring.service.AccountService.*(..)):切入点为执行AccountService类中所有方法时。:Aspect和Spring Aop 都支持
- execution(* com.jing.spring.service..(..)):切入点为执行com.jing.spring.service包下所有方法时。:Aspect和Spring Aop 都支持
- execution(* com.jing.spring.service...(..)):切入点为执行com.jing.spring.service包及其子包下的所有方法时。:Aspect和Spring Aop 都支持
- within:用于匹配指定类型内的方法。:only used by Spring AOP
- with(com.jing.spring.service.*)
- with(com.jing.spring.service..*)
- target:匹配当前目标对象类型的执行方法。:only used by Spring AOP
- args:匹配参数。:only used by Spring 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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean> <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> </aop:aspect> </aop:config> </beans>
- Next