如果只想拦截在某种情况下调用的方法,可以使用Spring AOP提供的ControlFlowPointcut来完成这个功能。比如,只有在MainTest类中的aaMethod方法中调用的目标类的方法才会被拦截,其它地方调用的目标类的方法将不会被拦截。
示例如下,应用上一篇日志的前置通知,不同就是这里应用ControlFlowPointcut,而上一篇日志应用了一个自定义的继承了NameMatchMethodPointcut的MyPointcut类。
本例相关的applicationContext.xml文件中的内容:
<!-- 定义前置通知 --> <bean id="beforeLogAdvice" class="com.aop.BeforeLogAdvice"></bean> <bean id="controlFlowPointcut" class="org.springframework.aop.support.ControlFlowPointcut"> <!-- 构造函数的参数 --> <constructor-arg index="0"> <value>com.test.MainTest</value> </constructor-arg> <constructor-arg index="1"> <value>aaMethod</value> </constructor-arg> </bean> <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut"> <ref local="controlFlowPointcut"/> </property> <property name="advice"> <ref local="beforeLogAdvice"/> </property> </bean>
beforeLogAdvice还是以前那个前置通知,DefaultPointcutAdvisor跟上一篇日志中的用法一样。controlFlowPointcut的bean里面,第一个构造函数参数就是拦截的目标类,第二个构造函数参数就是要拦截的方法名,也就的意思就是说只拦截com.test.MainTest下的aaMethod方法中调用的UserService类的所有方法。
MainTest测试类代码:
package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.IUserService; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); IUserService userService = (IUserService) context.getBean("myProxy"); userService.addUser("ton", 56); //不会被拦截 userService.deleteUser("ton");//不会被拦截 aaMethod(userService); } public static void aaMethod(IUserService userService){ userService.addUser("ton2", 56);//被拦截 userService.deleteUser("Alex");//被拦截 } }
相关推荐
开发工具 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-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-...
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
赠送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;...
赠送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...
赠送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;...
aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar
赠送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...
赠送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-3.2.5.RELEASE.jar
spring-aop-2.0.8.jar
spring-aop-4.0.4.RELEASE 的jar包,亲测可用。。。。
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...
赠送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.0.4.RELEASE.jar。
赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...
spring-**beans**-4.3.6.RELEASE.jar:所有应用都要用到的jar包,它包含访问配置文件.创建和管理bean以及进行Inversion ofContro(IOC )或者Dependency injection(DL)操作相关的所有类 spring-**cntext**-4.3.6....
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...
spring-aop-3.2.0.RELEASE.jar,一个Spring中AOP的jar包
aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等