zoukankan      html  css  js  c++  java
  • spring使用aop

    基于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/

  • 相关阅读:
    史上最大型广告欺诈活动Methbot:黑客是如何每天赚到500万美元的
    Google研究人员宣布完成全球首例SHA-1哈希碰撞!
    “无文件”恶意软件的威力:悄无声息一夜之间从ATM机中窃取80万美元
    怎样才能写出高质量的伪原创,并且排名在前?
    新手站长如何快速学习实践SEO?
    .net core 开发注意事项
    写cookies注意事项
    .net core Ocelot 开源的API网关学习
    .net core与asp.net读取网站根目录文件的区别
    asp.net 中webapi读取参数对象和.net core的区别
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/6227185.html
Copyright © 2011-2022 走看看