`

spring-aop-aspectj(Schema)-case

阅读更多
基于Schema 配置切面:
1、切点定义的语言依然是AspectJ表达式语言
2、把不用AspectJ相关的注解定义切面,而是通过xml配置,相当于注解的xml表达的aop定义形式。

定义advice增强
public class LoginMonitorAspectj {
	public static final ThreadLocal<Long> time = new ThreadLocal<Long>();

	public void before(JoinPoint pjp) {
		time.set(System.currentTimeMillis());
		System.out.println("invoke before --------------------------------");
		System.out.println(pjp.toLongString());
		System.out.println(Arrays.toString(pjp.getArgs()));
	}
	public void afterReturning(JoinPoint pjp,User user) {
		System.out.println("invoke afterReturning ---------------------------------");
		System.out.println(user.toString());
		System.out.println("times : "+(System.currentTimeMillis() - time.get()));
	}
	public void _exception(JoinPoint pjp,Exception ex) {
		System.out.println("invoke _exception ---------------------------------");
		System.out.println("times : "+(System.currentTimeMillis() - time.get()));
	}
}



业务方法定义
@Service
public class LoginService implements ILoginService {

	public User login(String username, String password) {
		User user = new User();
		System.out.println("in login method");
		user.setPassword("123456");
		user.setUsername("admin");
		return user;
	}

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

}


配置applicationContext.xml

<context:component-scan base-package="org.job">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<aop:aspectj-autoproxy/> 
	
	<bean id="adviceMethods" class="org.job.user.aop.LoginMonitorAspectj"/>
	
	<aop:config>
	   <aop:aspect ref="adviceMethods">
	        <aop:before pointcut="execution(* login(..))" method="before"/>
	        <aop:after-returning pointcut="execution(* login(..))" method="afterReturning" returning="user"/>
	        <aop:after-throwing pointcut="execution(* login(..))" method="_exception" throwing="ex"/>
	   </aop:aspect>
	</aop:config>
</beans>  


详细见附件
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-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.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-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.jar

    spring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.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-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....

    spring-aop-4.1.4.RELEASE.jar

    java spring AOP 支持添加的jar包,有需要的话可以下哦

Global site tag (gtag.js) - Google Analytics