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>


  • 相关阅读:
    CVPR2020论文解读:3D Object Detection三维目标检测
    CVPR2020论文介绍: 3D 目标检测高效算法
    自动驾驶感知系统盘点
    CVPR2020论文解析:实例分割算法
    HttpContext
    c# ExecuteScalar()
    C#中DBNull.Value和Null的用法和区别
    Guid
    CommandType.Text
    数据可视化基础专题(三):Pandas基础(二) csv导入与导出
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114735.html
Copyright © 2011-2022 走看看