新建一个AOP类:

public class MyInterceptor2 {
	public void doAccessCheck(){
		System.out.println("前置通知 ");
	}
	public void doAfterReturning(){
		System.out.println("后置通知 ");
	}
	public void doAfter(){
		System.out.println("最终通知");
	}
	public void doAround(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("环绕通知前");
		pjp.proceed();
		System.out.println("环绕通知后");
	}
	public void doAfterThrowing(Exception e){
		System.out.println("例外通知 例外 e:"+e);
	}
}

在beans.xml中注入,并配置AOP:

<aop:aspectj-autoproxy />
	<bean />
	<bean  />
	<aop:config>
		<!-- 定义切面 -->
		<aop:aspect >
			<!-- 定义切入点 -->
			<!-- 拦截所有返回值为String的方法:execution(java.lang.String cn.raffaello.service.impl.PersonServiceImpl.*(..)) -->
			<!-- 拦截所有返回值非void的方法:execution(!void cn.raffaello.service.impl.PersonServiceImpl.*(..)) -->
			<!-- 拦截第一个参数是String的方法:execution(* cn.raffaello.service.impl.PersonServiceImpl.*(java.lang.String,..)) -->
			<!-- 拦截包级子包下的所有类的所有的方法:execution(* cn.raffaello.service.*.*(..)) -->
			<!-- 拦截参数为String,并且参数名字为name: execution(* cn.raffaello.service.impl.PersonServiceImpl.*(String)) and args(name) -->
			<aop:pointcut  />
			<!-- 前置通知 -->
			<aop:before pointcut-ref="pointcut" method="doAccessCheck"/>
			<!-- 后置通知 -->
			<!-- 拦截返回值为String的方法 <aop:after-returning pointcut-ref="pointcut" method="doAfterReturning" returning="retv"/> -->
			<aop:after-returning pointcut-ref="pointcut" method="doAfterReturning" />
			<!-- 最终通知 -->
			<aop:after pointcut-ref="pointcut" method="doAfter" />
			<!-- 环绕通知 -->
			<aop:around pointcut-ref="pointcut" method="doAround" />
			<!-- 例外通知 -->
			<aop:after-throwing pointcut-ref="pointcut" method="doAfterThrowing" throwing="e"/>
		</aop:aspect>
	</aop:config>


相关文章: