spring整合aspectj实现aop的一个简单案例(xml)

1.************代理类***********************

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**      
 * @creationTime:2017年10月26日 上午12:07:22
 * @version: 1.1
 */
public class OrderServiceHelper {
	public void before(JoinPoint jp){
		System.out.println("拦截的目标类"+"\t"+jp.getSignature().getDeclaringTypeName());
		System.out.println("拦截的方法名称"+"\t"+jp.getSignature().getName());
		System.out.println("前置通知");
	}
	public void afterReturning(JoinPoint pjp,Object val){
		System.out.println("目标方法返回值"+val);
		System.out.println("后置通知");
	}
	public Object around(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("环绕前");
		Object value = pjp.proceed();
		System.out.println("环绕后");
		return value;
	}
	public void afterThrowing(JoinPoint jp,Throwable ex){
		System.out.println("发现异常..."+ex);
	}
	public void after(JoinPoint jp){
		System.out.println(jp.getSignature().getName());
		System.out.println("最终通知");
	}
}
2.*****************xml文件***************

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!--目标类-->
	<bean id="orderService" class="cn.xxxx.aspectj.OrderServiceImpl"></bean>
	<!--通知-->
	<bean id="orderServiceAdvice" class="cn.xxxx.aspectj.OrderServiceHelper"></bean>
	<!--使用aop:config来声明,使用 aop:aspect来配置切面(false表示在没有借口的情况下,默认使用cglib)-->
	<aop:config proxy-target-class="false">
		<aop:aspect ref="orderServiceAdvice">
			<!--切点-->
			<aop:pointcut expression="execution(* cn.xxxx.aspectj..*(..))" id="anyPointcut"/>
			<!--前置通知-->
			<aop:before method="before" pointcut="execution(* *..add(..))"/>
			<!--后置通知-->
			<aop:after-returning method="afterReturning" pointcut-ref="anyPointcut" returning="val"/>
			<!--环绕通知-->
			<aop:around method="around" pointcut-ref="anyPointcut"/>
			<!--异常抛出通知-->
			<aop:after-throwing method="afterThrowing" pointcut-ref="anyPointcut" throwing="ex"/>
			<!--最终通知-->
			<aop:after method="after" pointcut-ref="anyPointcut"/>
		</aop:aspect>
	</aop:config>

</beans>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值