spring使用aop

112 篇文章 0 订阅
18 篇文章 0 订阅

基于spring-framework-4.1.7使用aop

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2015年9月5日 23:46:28 星期六

http://fanshuyao.iteye.com/

 

一、spring中aop的使用需要的jar包:
1、aopalliance.jar
2、aspectjweaver-1.6.12.jar

 

3、commons-io-2.4.jar
4、commons-logging-1.2.jar

 

5、spring-aop-4.1.7.RELEASE.jar
6、spring-aspects-4.1.7.RELEASE.jar
7、spring-beans-4.1.7.RELEASE.jar
8、spring-context-4.1.7.RELEASE.jar
9、spring-core-4.1.7.RELEASE.jar
10、spring-expression-4.1.7.RELEASE.jar

 

备注:不需要使用apache中aspectj的jar包:aspectj-1.8.6.jar

 

二、springAop.xml配置
1、配置扫描包,把aop执行java类(AopLogging.java)用@Component注解,
然后用注解@Aspect声明该类为aop使用方式。
<context:component-scan base-package="com.spring.aop.*">
</context:component-scan>

2、接着声明使用aop代码
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 

三、在java类(AopLogging.java)中的方法使用注解声明通知。
通知有:
1、@Before 前置通知
2、@After 后置通知
3、@AfterReturning 结果通知
4、@AfterThrowing 异常通知
5、@Around 环绕通知(其实此通知为上面4个通知的集合)


然后在通知注解后添加“切点”
@Before("execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))")
@After("execution(* com.spring.aop.service.impl.CalculationServiceImpl.*)")

切点声明,即定义要使用aop的类或者类中的方法,可以用*来代替

 

 

注:附件中Java项目为无Jar包导出,需要在src目录下新建立一个lib文件夹,把jar放进去,

然后add to build path

 

 下面为aop的主要代码:

@Component
@Aspect
public class AopLogging {

	/**
	 * 前置通过,方法执行前执行
	 * @param joinpoint(org.aspectj.lang.JoinPoint;)
	 */
	@Before("execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))")
	public void beforeMethod(JoinPoint joinpoint){
		System.out.println("---------------the method: "+ joinpoint.getSignature().getName() + " is start.---------------");
		System.out.println("【@Before】the method called "+ joinpoint.getSignature().getName() + "'s args is "+ Arrays.asList(joinpoint.getArgs()));
	}
	
	/**
	 * @After 后置通知,不管程序有没有错,最后都会执行
	 * @param joinpoint
	 */
	@After("execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))")
	public void afterMethod(JoinPoint joinpoint){
		System.out.println("【@After】---------------the method: "+ joinpoint.getSignature().getName() + " is end.---------------");
	}
	
	/**
	 * @AfterReturning 结果通知,只有程序正常执行后才会返回结果通知
	 * @param joinpoint
	 * @param obj 对应returning="obj"的obj,名称一样
	 */
	@AfterReturning(value="execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))",
			returning="obj")
	public void returnMethod(JoinPoint joinpoint, Object obj){
		System.out.println("【@AfterReturning】the method called "+ joinpoint.getSignature().getName() + "'s result is " + obj);
	}
	
	/**
	 * @AfterThrowing 异常通知,程序产生异常后(符合异常抓取规则)执行
	 * @param joinpoint
	 * @param obj 对应throwing="obj"的obj,名称一样
	 */
	@AfterThrowing(value="execution(public int com.spring.aop.service.impl.CalculationServiceImpl.*(int, int))",
			throwing="obj")
	public void throwingMethod(JoinPoint joinpoint, Object obj){
		System.out.println("【@AfterThrowing】the method called "+ joinpoint.getSignature().getName() + " is throw Exeception:" + obj);
	}
	
}

 

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2015年9月5日 23:46:28 星期六

http://fanshuyao.iteye.com/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值