Spring框架AOP增强处理

tech2022-09-29  101

增强处理分为五种  前置增强   后置增强   异常增强   最终增强   环绕增强  

使用它们有两种方式  注解和标签

首先演示标签

前置增强

         示例

                    第一步在aop类定义前置增强

//Logger 是某个静态类,这个类里面应该有一个logger属性,该属性对应的get和set方法,Logger.getLogger(“”)是直接调用类方法 private static final Logger log=Logger.getLogger(app.class); //@Before("execution(* com.hhb.sping(..))") //前置增强方法 该方法添加到目标之前执行 //JoinPoint为了能够在增强方法中获得当前连接点信息,以便实施相关的判断和处理,可以在增强方法中声明JoinPoint类型参数Spring会自动注入该实例 //getTarget()可以获得到代理的目标对象 //getSignature()返回被代理的目标方法 //getArgs()返回传递给目标方法的参数数组 public void before(JoinPoint jp){ System.out.println("sdssasda"); log.info("前置增强,调用"+jp.getTarget()+"的"+jp.getSignature() .getName()+"方法."+"方法参数:"+Arrays.toString(jp.getArgs())); }

 第二步在applicationContext.xml

引用app类 <bean id="app" class="com.hhb.aop.app"></bean> 使用切面标签 <aop:config> <aop:pointcut expression="execution (* com.hhb.serivce.Impl.*.*(..))" id="res"/>切面点 <aop:aspect ref="app"> <aop:before method="before" pointcut-ref="res"/>前置增强 </aop:aspect> </aop:config> 后置增强

         第一步在aop类定义后置增强

//@AfterReturning(pointcut="execution(* com.hhb.sping.*(..))",returning="resuet") //后置增强方法 该方法添加到目标方法正常返回之后执行 public void afterReturning(JoinPoint jp,Object resuet){ log.info("后置增强,调用"+jp.getTarget()+"的"+jp.getSignature() .getName()+"方法"+"方法返回值:"+resuet); }

 第二步在applicationContext.xml

引用app类 <bean id="app" class="com.hhb.aop.app"></bean> 使用切面标签 <aop:config> <aop:pointcut expression="execution (* com.hhb.serivce.Impl.*.*(..))" id="res"/>切面点 <aop:aspect ref="app"> <aop:before method="before" pointcut-ref="res"/>前置增强 <aop:after-returning method="afterReturning" pointcut-ref="res" returning="resuet"/>后置增强 </aop:aspect> </aop:config> 异常增强

第一步在aop类定义异常增强

public void throwss(JoinPoint jp,RuntimeException t){ log.info("异常增强 ,调用" + jp.getTarget() + "类,下的方法是" + jp.getSignature().getName() + "出现的异常是:" +t); }

      第二步在applicationContext.xml

<!-- 引用app类 --> <bean id="app" class="com.hhb.aop.app"></bean> <!-- 使用切面标签 --> <aop:config> <aop:pointcut expression="execution (* com.hhb.serivce.Impl.*.*(..))" id="res"/><!-- 切面点 --> <aop:aspect ref="app"> <aop:before method="before" pointcut-ref="res"/><!-- 前置增强 --> <aop:after-returning method="afterReturning" pointcut-ref="res" returning="resuet"/><!-- 后置增强 --> <aop:after-throwing method="throwss" pointcut-ref="res" throwing="t"/><!-- 异常增强 --> </aop:aspect> </aop:config> 最终增强

第一步在aop类定义最终增强

public void after(JoinPoint jp){ log.info("最终增强,调用" + jp.getTarget() + "类,下的方法是" + jp.getSignature().getName() + ",方法的参数是:" + Arrays.toString(jp.getArgs())); }

第二步在applicationContext.xml

<!-- 引用app类 --> <bean id="app" class="com.hhb.aop.app"></bean> <!-- 使用切面标签 --> <aop:config> <aop:pointcut expression="execution (* com.hhb.serivce.Impl.*.*(..))" id="res"/><!-- 切面点 --> <aop:aspect ref="app"> <aop:before method="before" pointcut-ref="res"/><!-- 前置增强 --> <aop:after-returning method="afterReturning" pointcut-ref="res" returning="resuet"/><!-- 后置增强 --> <aop:after-throwing method="throwss" pointcut-ref="res" throwing="t"/><!-- 异常增强 --> <aop:after method="after" pointcut-ref="res"/><!-- 最终增强 --> </aop:aspect> </aop:config> 环绕增强

            包含了所有增强方法

     第一步在aop类定义环绕增强

public Object around(ProceedingJoinPoint joinPoint) { Object result = null; try { log.info("前置增强 ,调用" + joinPoint.getTarget() + "类,下的方法是" + joinPoint.getSignature().getName() + ",方法的参数是:" + Arrays.toString(joinPoint.getArgs())); result = joinPoint.proceed(); log.info(" 后置增强,调用" + joinPoint.getTarget() + "类,下的方法是" + joinPoint.getSignature().getName() + ",方法的参数是:" + Arrays.toString(joinPoint.getArgs()) + ",方法的返回值是:" + result); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); log.info("异常增强 ,调用" + joinPoint.getTarget() + "类,下的方法是" + joinPoint.getSignature().getName() + "出现的异常是:" + e); } finally { log.info("最终增强,调用" + joinPoint.getTarget() + "类,下的方法是" + joinPoint.getSignature().getName() + ",方法的参数是:" + Arrays.toString(joinPoint.getArgs())); } return result; }

第二步在applicationContext.xml

<!-- 引用app类 --> <bean id="app" class="com.hhb.aop.app"></bean> <!-- 使用切面标签 --> <aop:config> <aop:pointcut expression="execution (* com.hhb.serivce.Impl.*.*(..))" id="res"/><!-- 切面点 --> <aop:aspect ref="app"> <aop:around method="around" pointcut-ref="res"/> </aop:aspect> </aop:config>

注解增强

第一步先在aop声明注解

 

前置增强

@Before("execution(* com.hhb.sping.*(..))") //前置增强方法 该方法添加到目标之前执行 //JoinPoint为了能够在增强方法中获得当前连接点信息,以便实施相关的判断和处理,可以在增强方法中声明JoinPoint类型参数Spring会自动注入该实例 //getTarget()可以获得到代理的目标对象 //getSignature()返回被代理的目标方法 //getArgs()返回传递给目标方法的参数数组 public void before(JoinPoint jp){ System.out.println("sdssasda"); log.info("前置增强,调用"+jp.getTarget()+"的"+jp.getSignature() .getName()+"方法."+"方法参数:"+Arrays.toString(jp.getArgs())); }

 后置增强

@AfterReturning(pointcut="execution(* com.hhb.sping.*(..))",returning="resuet") //后置增强方法 该方法添加到目标方法正常返回之后执行 public void afterReturning(JoinPoint jp,Object resuet){ log.info("后置增强,调用"+jp.getTarget()+"的"+jp.getSignature() .getName()+"方法"+"方法返回值:"+resuet); }

异常增强

@AfterThrowing(pointcut="execution(* com.hhb.sping.*(..))",throwing="t") //异常增强 public void throwss(JoinPoint jp,RuntimeException t){ log.info("异常增强 ,调用" + jp.getTarget() + "类,下的方法是" + jp.getSignature().getName() + "出现的异常是:" +t); }

 最终增强

@After("execution(* com.hhb.sping.*(..))") //最终增强 public void after(JoinPoint jp){ log.info("最终增强,调用" + jp.getTarget() + "类,下的方法是" + jp.getSignature().getName() + ",方法的参数是:" + Arrays.toString(jp.getArgs())); }

 环绕增强

@Around("execution(* com.hhb.sping.*(..))") //环绕增强 public Object around(ProceedingJoinPoint jp){ log.info("前置增强,调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法"+"方法。方法参数:"+Arrays.toString(jp.getArgs())); Object result=null; try { log.info("后置增强,调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法"+"方法。方法参数:"+Arrays.toString(jp.getArgs())); return jp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); log.info("异常,调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法"+"方法。方法参数:"+Arrays.toString(jp.getArgs())); } return result; }

 在applicationContext.xml配置文件中只需两行代码就足够

第一步首先勾选applicationContext.xm中Namespaces命名空间的所勾选项这样才可以创建<aop>标签

<context:component-scan base-package="com.hhb.sping"></context:component-scan> <bean class="com.hhb.sping.app"> <aop:aspectj-autoproxy />

 

最新回复(0)