`

spring-aop-aspectj-case

阅读更多
AOP概念:面向切面编程。

spring 集成AOP:
1、spring 有自己的aop实现ProxyFactory ,和 ProxyFactoryBean 。
2、spring 集成AspectJ实现的Aop。

spring AOP的使用场景:
在spring中 事务处理和rpc调用都大量的使用了aop,在低入侵监控方面都有大量应用。

spring以来的底层技术:Jdk代理和Cglib代理技术。

spring 支持的aop应用实现方式有以下几种:
1、基于ProxyFactory 的编程方式的aop实现。
2、基于ProxyFactoryBean 配置的,走spring ioc 路的实现方式。
3、基于以上方式自动创建代理 BeanNameAutoProxyCreator +Advice 和 DefaultAdvisorAutoProxyCreator + Advisor方式。
4、基于AspectJProxyFactory + AspectJ注解的编程的方式。
5、基于AspectJ 注解 + AnnotationAwareAspectJAutoProxyCreator的方式。
6、基于Schema + Aspectj注解语言的 Aop:config方式。


下面举例 基于AspectJ 注解的方式,实现一个方法耗时统计的功能:

/**
 * aspectj 基于注解的方式
 * @author wangxinchun
 */
public @interface MethodExecuteTimes {}



定义需要代理的业务方法
@Service
public class LoginService implements ILoginService {

	@MethodExecuteTimes
	public void regist(String username, String password, String email, int age) {
		   System.out.println(Arrays.toString(new Object[]{username,password,age}));
	}

}


定义切面
package org.job.user.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * 定义切面 (关注方法上有注解:org.job.user.MethodExecuteTimes)
 * @author wangxinchun
 */
@Aspect
@Component
public class RegisterMonitorAspectj {
	public static final ThreadLocal<Long> time = new ThreadLocal<Long>();
	/** 方法调用前执行*/
	@Before("@annotation(org.job.user.MethodExecuteTimes))")
	public void before() {
		time.set(System.currentTimeMillis());
		System.out.println("invoke before ");
	}
	/** 方法调用后执行(如果方法抛出异常,此方法不会执行)*/
	@AfterReturning("@annotation(org.job.user.MethodExecuteTimes))")
	public void after() {
		System.out.println("invoke after ");
		System.out.println("times : "+(System.currentTimeMillis() - time.get()));
	}
	/** 方法调用finally 执行*/
	@After("@annotation(org.job.user.MethodExecuteTimes))")
	public void _final(){
		System.out.println("final");
	}
}


applicationContext.xml 配置启动AspectJ
<!-- <aop:aspectj-autoproxy/> -->
	<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>


测试用例
@Test
	public void testRegister() {
		LoginService loginService = new LoginService();
		AspectJProxyFactory factory = new AspectJProxyFactory();
		factory.setTarget(loginService);
		factory.addAspect(RegisterMonitorAspectj.class);
		
		
		ILoginService proxy = factory.getProxy();
		proxy.regist("admin", "123456","xinchun.wang@qunar.com",10);
	}


具体实现看附件~
0
0
分享到:
评论

相关推荐

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    spring-aop-jar

    aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar

    spring-aop-3.2.5.RELEASE.jar ;spring-aop-3.2.5.jar

    spring-aop-3.2.5.RELEASE.jar

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    spring-aop-4.0.4.RELEASE

    spring-aop-4.0.4.RELEASE 的jar包,亲测可用。。。。

    spring-aop-5.0.4.RELEASE.jar

    spring-aop-5.0.4.RELEASE.jar。

    spring-aop-3.2.0.RELEASE.jar

    spring-aop-3.2.0.RELEASE.jar,一个Spring中AOP的jar包

    spring-aop-5.1.0.RELEASE.jar

    spring-**core**-4.3.6.RELEASE.jar :包含spring框架基本的核心工具类,spring其他组件都要用到这个包里的类,其他组件的基本核心 spring-**beans**-4.3.6.RELEASE.jar:所有应用都要用到的jar包,它包含访问配置...

    spring-aop-2.0.8.jar

    spring-aop-2.0.8.jar

    spring-aop-5.0.1.RELEASE.jar

    spring-aop-5.0.1.RELEASE.jar

    spring-aop.jar

    spring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jar

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

Global site tag (gtag.js) - Google Analytics