• 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>


  • 相关阅读:
    log4j的使用
    转:http与https
    转:归一化与正则化
    转:python 的开源库
    转:openTSDB 2.0 安装
    hadoop 2.2.0编译安装及打包
    查看磁盘信息
    HBase Region的flush过程
    HBase96的RPC
    阐述二维码的原理以及使用google api和PHP QR Code来生成二维码
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114735.html
Copyright © 2020-2023  润新知