Spring学习笔记(16)----使用Spring配置文件实现AOP
来源:互联网 发布:网络维护做点什么 编辑:程序博客网 时间:2024/05/01 11:54
前面介绍了使用注解的方式,下面介绍使用配置文件的方式实现AOP。
使用配置方式,Interceptor类中不包含任何注解。
package com.szy.spring;import org.aspectj.lang.ProceedingJoinPoint;public class Interceptor{public void doBefore(){System.out.println("----------------执行前置通知-----------------");}public void doAfterReturning(){System.out.println("----------------执行后置通知-----------------");}public void doAfter(){System.out.println("----------------执行最终通知-----------------");}public void doAfterThrowing(){System.out.println("----------------执行意外通知-----------------");}public Object doAround(ProceedingJoinPoint pjp) throws Throwable{System.out.println("----------------进入判断方法-----------------");Object result=pjp.proceed(); //该方法必须被执行System.out.println("----------------退出判断方法-----------------");return result;}}
紧着这我们在配置文件中配置切面、切入点、通知等:
<bean id="aspetbean" class="com.szy.spring.Interceptor"/><aop:config><aop:aspect id="aspet" ref="aspetbean"><aop:pointcut id="cut" expression="execution (* com.szy.spring.UserManagerImpl.*(..))"/><aop:before pointcut-ref="cut" method="doBefore"/><aop:after-returning pointcut-ref="cut" method="doAfterReturning"/><aop:after pointcut-ref="cut" method="doAfter"/><aop:after-throwing pointcut-ref="cut" method="doAfterThrowing"/><aop:around pointcut-ref="cut" method="doAround"/></aop:aspect></aop:config>
运行测试代码输入正常结果。
在实际开发中AOP一般用于权限设置等。