博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring aop之对象内部方法间的嵌套失效
阅读量:2036 次
发布时间:2019-04-28

本文共 8409 字,大约阅读时间需要 28 分钟。

spring aop之对象内部方法间的嵌套失效

先看一下spring 代理原理:

*       spring代理嵌套方法调用不生效
 * 
 *       Spring AOP defaults to using standard JDK dynamic proxies for AOP
 *       proxies. This enables any interface (or set of interfaces) to be
 *       proxied.
 * 
 *       Spring AOP can also use CGLIB proxies. This is necessary to proxy
 *       classes rather than interfaces. CGLIB is used by default if a business
 *       object does not implement an interface. As it is good practice to
 *       program to interfaces rather than classes; business classes normally
 *       will implement one or more business interfaces. It is possible to force
 *       the use of CGLIB, in those (hopefully rare) cases where you need to
 *       advise a method that is not declared on an interface, or where you need
 *       to pass a proxied object to a method as a concrete type.
 * 
 *       It is important to grasp the fact that Spring AOP is proxy-based. See
 *       Section 11.6.1, “Understanding AOP proxies” for a thorough examination
 *       of exactly what this implementation detail actually means.
 * 
 * 
 * 
 *       通过配置 <aop:aspectj-autoproxy proxy-target-class="true" />可以统一用CGLIB
 *       proxies。
 * 
实际上注入到spring容器的类实例是代理类,自然我们调用类的方法都能被aop拦截,但是类中内部方法的调用即this.metho(...), 这里的this,并不是spirng生成的代理类实例,而是实际产生的原对象,自然,aop就不会拦截这样的方法调用。
写个简短的程序验证一下spring代理嵌套方法调用不生效。
package com.doctor.aop.demo;import org.springframework.stereotype.Component;/** * @description  * * @author sdcuike * * @date 2016年6月24日 下午6:44:32 */@Componentpublic class Demo {	public void test01() {		System.out.println("test1");		System.out.println("this对象指的不是代理类对象,而是原对象" + this.getClass());		test02("hello nest ");	}		public String test02(String name) {		return name;	}}
package com.doctor.aop.demo;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @description * * @author sdcuike * * @date 2016年6月24日 下午6:44:12 *  *       spring代理嵌套方法调用不生效 *  *       Spring AOP defaults to using standard JDK dynamic proxies for AOP *       proxies. This enables any interface (or set of interfaces) to be *       proxied. *  *       Spring AOP can also use CGLIB proxies. This is necessary to proxy *       classes rather than interfaces. CGLIB is used by default if a business *       object does not implement an interface. As it is good practice to *       program to interfaces rather than classes; business classes normally *       will implement one or more business interfaces. It is possible to force *       the use of CGLIB, in those (hopefully rare) cases where you need to *       advise a method that is not declared on an interface, or where you need *       to pass a proxied object to a method as a concrete type. *  *       It is important to grasp the fact that Spring AOP is proxy-based. See *       Section 11.6.1, “Understanding AOP proxies” for a thorough examination *       of exactly what this implementation detail actually means. *  *  *  *       通过配置 
可以统一用CGLIB * proxies。 * */public class MethodInvokeTimeAspectTest { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:/aopDemo1/spring-aop.xml"); Demo demo = applicationContext.getBean(Demo.class); demo.test01(); demo.test02("doctor who"); System.out.println("注入到spring容器的类实例是代理类" + demo.getClass()); // class com.doctor.aop.demo.Demo$$EnhancerBySpringCGLIB$$e9a9050a // 注入到spring容器的类实例是代理类 InterfaceDemo interfaceDemo = applicationContext.getBean(InterfaceDemo.class); System.out.println("注入到spring容器的类实例是代理类" + interfaceDemo.getClass()); interfaceDemo.testAnother(); applicationContext.close(); }}
看一下输出:
07-03 11:23:57.357 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@9f70c54: startup date [Sun Jul 03 11:23:57 CST 2016]; root of context hierarchy07-03 11:23:57.457 main  INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader -	Loading XML bean definitions from class path resource [aopDemo1/spring-aop.xml]AspectAopDemo===before  execution(void com.doctor.aop.demo.Demo.test01())test1this对象指的不是代理类对象,而是原对象class com.doctor.aop.demo.DemoAspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))AspectAopDemo===after   execution(void com.doctor.aop.demo.Demo.test01())07-03 11:23:59.108 main  INFO  com.doctor.aop.demo.MethodInvokeTimeAspect -	[execution(void com.doctor.aop.demo.Demo.test01())],[],[null], [20] AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))07-03 11:23:59.108 main  INFO  com.doctor.aop.demo.MethodInvokeTimeAspect -	[execution(String com.doctor.aop.demo.Demo.test02(String))],[doctor who],[doctor who], [0] 注入到spring容器的类实例是代理类class com.doctor.aop.demo.Demo$$EnhancerBySpringCGLIB$$595bc0d7注入到spring容器的类实例是代理类class com.sun.proxy.$Proxy707-03 11:23:59.108 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Closing org.springframework.context.support.ClassPathXmlApplicationContext@9f70c54: startup date [Sun Jul 03 11:23:57 CST 2016]; root of context hierarchy
日志结果:注入到spring容器内的类实例是代理类实例,非原实例,而类内方法之间调用的时候,this指向的是原实例,非代理类。
由spring代理是动态的,运行时织入,固类内部方法之间调用的时候,不可能让this指向代理类。
源码地址:

如何解决

 动态代理不行,我们基于字节码的支持编译期间进行织入(weaving),还是编译后(post-compile)的AspectJ。
package com.doctor.aop.aspect.demo;/** * @description  * * @author sdcuike * * @date 2016年6月27日 下午1:51:14 */public aspect AspectAopDemo {pointcut demo() : execution(* com.doctor.aop.demo.Demo.* (..));before() : demo() {	System.out.println("AspectAopDemo===before  " + thisJoinPoint);}after() : demo() {	System.out.println("AspectAopDemo===after   " + thisJoinPoint); }}
package com.doctor.aop.demo;import org.springframework.stereotype.Component;/** * @description  * * @author sdcuike * * @date 2016年6月24日 下午6:44:32 */@Componentpublic class Demo {	public void test01() {		System.out.println("test1");		System.out.println("this对象指的不是代理类对象,而是原对象" + this.getClass());		test02("hello nest ");	}		public String test02(String name) {		return name;	}}
看一下结果:eclipse IDE中把项目转变成AspectJ项目。
运行下面程序:(AspectJ/java application)
package com.doctor.aop.aspect.demo;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.doctor.aop.demo.Demo;/** * @description * * @author sdcuike * * @date 2016年6月27日 下午2:16:57 *  *       aspect 编译时候注入,克服动态代理方法嵌套调用失效问题. *  *       运行方法:把项目转换成AspectJ工程,运行的时候选择:Run As-> AspectJ/Java Application */class AspectAopDemoTest {	public static void main(String[] args) {		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(				"classpath:/aopDemo1/spring-aop2.xml");		Demo demo = applicationContext.getBean(Demo.class);		demo.test01();		demo.test02("doctor who");		System.out.println("注入到spring容器的类实例是代理类" + demo.getClass());		// class com.doctor.aop.demo.Demo$$EnhancerBySpringCGLIB$$e9a9050a		// 注入到spring容器的类实例是代理类		applicationContext.close();	}}
输出结果:
07-03 12:04:48.375 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13c78c0b: startup date [Sun Jul 03 12:04:48 CST 2016]; root of context hierarchy07-03 12:04:48.421 main  INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader -	Loading XML bean definitions from class path resource [aopDemo1/spring-aop2.xml]AspectAopDemo===before  execution(void com.doctor.aop.demo.Demo.test01())test1this对象指的不是代理类对象,而是原对象class com.doctor.aop.demo.DemoAspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))AspectAopDemo===after   execution(void com.doctor.aop.demo.Demo.test01())AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))注入到spring容器的类实例是代理类class com.doctor.aop.demo.Demo07-03 12:04:48.794 main  INFO  org.springframework.context.support.ClassPathXmlApplicationContext -	Closing org.springframework.context.support.ClassPathXmlApplicationContext@13c78c0b: startup date [Sun Jul 03 12:04:48 CST 2016]; root of context hierarchy
test02方法确实被调用了4次。
使用AspectJ的一个间接局限是,因为AspectJ通知可以应用于POJO之上,它有可能将通知应用于一个已配置的通知之上。对于一个你没有注意到这方面问题的大范围应用的通知,这有可能导致一个无限循环。
具体更详细的参考:

转载地址:http://steaf.baihongyu.com/

你可能感兴趣的文章
加速页面显示 压缩html js css
查看>>
Spring MVC 学习笔记 data binding conversionService
查看>>
eclipse里查看一个接口的所有实现类
查看>>
导入导出Excel工具类ExcelUtil
查看>>
excel poi 设置列宽度
查看>>
jquery ajax缓存问题解决方法小结
查看>>
Spring并发访问的线程安全性问题
查看>>
java 获取HttpRequest Header 的几种方法
查看>>
SpringMVC在Controller层中注入request的坑
查看>>
Spring事务总结---事务概述及Spring事务的基本使用(完整)
查看>>
子类可以继承到父类上的注解吗--有结论了
查看>>
Spring事务总结---传播级别以及REQUIRED_NEW及NESTED的使用场景(赞)
查看>>
通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
查看>>
spring 默认事务传播属性
查看>>
shutdown和shutdownNow--多线程任务的关闭
查看>>
JVM实用参数(七)CMS收集器
查看>>
nginx 已有80端口服务如何在开启一个非80端口的静态资源指向
查看>>
nginx root静态资源地址默认路径
查看>>
redis多个线程操作单个key场景的并发问题
查看>>
textArea没有value 属性 如何显示
查看>>