第十章 Spring AOP之通知


AOP之通知:

通过配置spring,将通知切入在方法执行前或后,添加打印日志信息,异常信息,获取类名,方法名,参数等

1,前置通知;
2,后置通知;
3,环绕通知;
4,返回通知;
5,异常通知;



<?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">
	
	<!-- 定义通知 -->
	<bean id="studentServiceAspect" class="com.fx.advice.StudentServiceAspect"></bean>
	
	<!-- 定义业务类 -->
	<bean id="studentService" class="com.fx.service.impl.StudentServiceImpl"></bean>
	
	<!-- 业务类配置通知 -->
	<aop:config>
		<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
			<!-- 切点表达式,匹配包路径下的java -->
			<aop:pointcut expression="execution(* com.fx.service.*.*(..))" id="businessService"/>
			<!-- 前置通知 -->
			<aop:before method="doBefore" pointcut-ref="businessService"/>
			<!-- 后置通知 -->
			<aop:after method="doAfter" pointcut-ref="businessService"/>
			<!-- 环绕通知 -->
			<aop:around method="doAround" pointcut-ref="businessService"/>
			<!-- 返回通知 -->
			<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
			<!-- 异常通知 -->
			<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
		</aop:aspect> 
	</aop:config>
</beans>

猜你喜欢

转载自1151461406.iteye.com/blog/2390238