Spring AOP编程(两种:1,传统的AOP切面编程 2,基于aspectj框架切点传统开发)
第一种,Spring传统AOP编程开发
在传统的spring aop中有5种对类中方法的增强
1,前置通知 在目标方法之前来执行对类中方法的增强
2,后置通知 在目标方法执行之后来执行方法增强
3,环绕通知 在目标方法执行前后抛出方法的增强
4,异常抛出通知在发生异常的时候抛出对方法的增强
5,引介通知 在目标类中添加一些新的方法或属性,开发中不常用
Spring传统AOP的实现流程
1,创建通知类
2,配置文件
<!-- 目标target -->
<bean id="aoptest01"class="cmo.demo.aop.OrderService"></bean>
<!-- 通知advice -->
<bean id="aopadvice"class="cmo.demo.aop.AOPAdvice"></bean>
<!-- 定义切点,用两种方式 -->
<!-- 第一种,用对应方法名来操作 -->
<bean id="aopOpthin"class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedNames">
<list>
<value>addTest</value>
<value>updateTest</value>
</list>
</property>
</bean>
<!-- 第二种,用表达式来限制方法的操作 -->
<!-- <beanid="aopOpthin" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<propertyname="pattern" value=".*Test"></property>
</bean>
-->
<!-- 定义切面,切点+通知 -->
<bean id="PointCut"class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice"ref="aopadvice"></property>
<property name="pointcut"ref="aopOpthin"></property>
</bean>
<!-- 代理动态proxy,目标对象,切面,代理对象-->
<bean id="proxy"class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"ref="aoptest01"></property>
<property name="interceptorNames"value="PointCut"></property>
<property name="proxyInterfaces"value="cmo.demo.aop.IOrderService"></property>
</bean>
</beans>
重点注意:生成动态标签中的id,要设置成和test测试类的,通 @Autowired
@Qualifier("proxy")
public IOrderService orderService;
来使用代理对象
关于切点表达式写法
1. execution(public * *()) 所有的public的方法
2. execution(*cn.itheima.aop.*(..)) 所有的aop包下的所有类的方法(不包含子包)
3. execution(*cn.itheima.aop..*(..)) 所有的aop包及其子包下的所有类的方法
4. execution(*cn.itheima.aop.IOrderService.*(..)) IOrderService接口中定义的所有方法
5. execution(*cn.itheima.aop.IOrderService+.*(..)) 匹配实现特定接口所有类的方法
6. execution(*save*(..)) 区配所有的以save开头的方法