zoukankan      html  css  js  c++  java
  • Spring、XML配置AOP

    新建一个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 id="personIService" class="cn.raffaello.service.impl.PersonServiceImpl"/>
    	<bean id="myInterceptor2" class="cn.raffaello.aop.MyInterceptor2" />
    	<aop:config>
    		<!-- 定义切面 -->
    		<aop:aspect id="aspect" ref="myInterceptor2">
    			<!-- 定义切入点 -->
    			<!-- 拦截所有返回值为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 id="pointcut" expression="execution(* cn.raffaello.service.impl.PersonServiceImpl.*(java.lang.String))" />
    			<!-- 前置通知 -->
    			<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>


  • 相关阅读:
    QuickStart系列:docker部署之Gitlab本地代码仓库
    https环境搭建(本地搭建)
    docker搭建elk
    使用本机IP调试web项目
    VC++ 异常处理 __try __except的用法
    Delphi编程常用快捷键大全
    Delphi2007安装报Invalid Serial Number问题
    Cannot create file "C:UsersADMINI~1AppDataLocalTempEditorLineEnds.ttr"
    delphi 调试的时候变量全部显示Inaccessible value的解决办法
    Delphi idhttp解决获取UTF-8网页中文乱码问题
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114735.html
Copyright © 2011-2022 走看看