Spring框架AOP增强处理

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

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

首先演示标签

  1. 前置增强

         示例

                    第步在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>
  1. 后置增强

         第步在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>
  1. 异常增强

第一步在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>
  1. 最终增强

第一步在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>
  1. 环绕增强

            包含了所有增强方法

     第一步在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 />

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值