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代理嵌套方法调用不生效。
  1. package com.doctor.aop.demo;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. /** 
  6.  * @description  
  7.  * 
  8.  * @author sdcuike 
  9.  * 
  10.  * @date 2016年6月24日 下午6:44:32 
  11.  */  
  12. @Component  
  13. public class Demo {  
  14.     public void test01() {  
  15.         System.out.println("test1");  
  16.         System.out.println("this对象指的不是代理类对象,而是原对象" + this.getClass());  
  17.         test02("hello nest ");  
  18.     }  
  19.       
  20.     public String test02(String name) {  
  21.         return name;  
  22.     }  
  23. }  
package com.doctor.aop.demo;

import org.springframework.stereotype.Component;

/**
 * @description 
 *
 * @author sdcuike
 *
 * @date 2016年6月24日 下午6:44:32
 */
@Component
public 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;
	}
}

  1. package com.doctor.aop.demo;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. /** 
  6.  * @description 
  7.  * 
  8.  * @author sdcuike 
  9.  * 
  10.  * @date 2016年6月24日 下午6:44:12 
  11.  *  
  12.  *       spring代理嵌套方法调用不生效 
  13.  *  
  14.  *       Spring AOP defaults to using standard JDK dynamic proxies for AOP 
  15.  *       proxies. This enables any interface (or set of interfaces) to be 
  16.  *       proxied. 
  17.  *  
  18.  *       Spring AOP can also use CGLIB proxies. This is necessary to proxy 
  19.  *       classes rather than interfaces. CGLIB is used by default if a business 
  20.  *       object does not implement an interface. As it is good practice to 
  21.  *       program to interfaces rather than classes; business classes normally 
  22.  *       will implement one or more business interfaces. It is possible to force 
  23.  *       the use of CGLIB, in those (hopefully rare) cases where you need to 
  24.  *       advise a method that is not declared on an interface, or where you need 
  25.  *       to pass a proxied object to a method as a concrete type. 
  26.  *  
  27.  *       It is important to grasp the fact that Spring AOP is proxy-based. See 
  28.  *       Section 11.6.1, “Understanding AOP proxies” for a thorough examination 
  29.  *       of exactly what this implementation detail actually means. 
  30.  *  
  31.  *  
  32.  *  
  33.  *       通过配置 <aop:aspectj-autoproxy proxy-target-class="true" />可以统一用CGLIB 
  34.  *       proxies。 
  35.  *  
  36.  */  
  37. public class MethodInvokeTimeAspectTest {  
  38.   
  39.     public static void main(String[] args) {  
  40.         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(  
  41.                 "classpath:/aopDemo1/spring-aop.xml");  
  42.         Demo demo = applicationContext.getBean(Demo.class);  
  43.         demo.test01();  
  44.         demo.test02("doctor who");  
  45.         System.out.println("注入到spring容器的类实例是代理类" + demo.getClass());  
  46.         // class com.doctor.aop.demo.Demo
    EnhancerBySpringCGLIB 
    e9a9050a  
  47.         // 注入到spring容器的类实例是代理类  
  48.   
  49.         InterfaceDemo interfaceDemo = applicationContext.getBean(InterfaceDemo.class);  
  50.         System.out.println("注入到spring容器的类实例是代理类" + interfaceDemo.getClass());  
  51.         interfaceDemo.testAnother();  
  52.         applicationContext.close();  
  53.     }  
  54.   
  55. }  
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.
 * 
 * 
 * 
 *       通过配置 <aop:aspectj-autoproxy proxy-target-class="true" />可以统一用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();
	}

}

看一下输出:
  1. 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 hierarchy  
  2. 07-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]  
  3. AspectAopDemo===before  execution(void com.doctor.aop.demo.Demo.test01())  
  4. test1  
  5. this对象指的不是代理类对象,而是原对象class com.doctor.aop.demo.Demo  
  6. AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))  
  7. AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))  
  8. AspectAopDemo===after   execution(void com.doctor.aop.demo.Demo.test01())  
  9. 07-03 11:23:59.108 main  INFO  com.doctor.aop.demo.MethodInvokeTimeAspect - [execution(void com.doctor.aop.demo.Demo.test01())],[],[null], [20]   
  10. AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))  
  11. AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))  
  12. 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]   
  13. 注入到spring容器的类实例是代理类class com.doctor.aop.demo.Demo
    EnhancerBySpringCGLIB 
    595bc0d7  
  14. 注入到spring容器的类实例是代理类class com.sun.proxy.$Proxy7  
  15. 07-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  
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 hierarchy
07-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())
test1
this对象指的不是代理类对象,而是原对象class com.doctor.aop.demo.Demo
AspectAopDemo===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.$Proxy7
07-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。

  1. package com.doctor.aop.aspect.demo;  
  2.   
  3. /** 
  4.  * @description  
  5.  * 
  6.  * @author sdcuike 
  7.  * 
  8.  * @date 2016年6月27日 下午1:51:14 
  9.  */  
  10. public aspect AspectAopDemo {  
  11. pointcut demo() : execution(* com.doctor.aop.demo.Demo.* (..));  
  12.   
  13. before() : demo() {  
  14.     System.out.println("AspectAopDemo===before  " + thisJoinPoint);  
  15. }  
  16.   
  17. after() : demo() {  
  18.     System.out.println("AspectAopDemo===after   " + thisJoinPoint);  
  19.  }  
  20. }  
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);
 }
}

  1. package com.doctor.aop.demo;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. /**  
  6.  * @description   
  7.  *  
  8.  * @author sdcuike  
  9.  *  
  10.  * @date 2016年6月24日 下午6:44:32  
  11.  */  
  12. @Component  
  13. public class Demo {  
  14.     public void test01() {  
  15.         System.out.println("test1");  
  16.         System.out.println("this对象指的不是代理类对象,而是原对象" + this.getClass());  
  17.         test02("hello nest ");  
  18.     }  
  19.       
  20.     public String test02(String name) {  
  21.         return name;  
  22.     }  
  23. }  
package com.doctor.aop.demo;

import org.springframework.stereotype.Component;

/**
 * @description 
 *
 * @author sdcuike
 *
 * @date 2016年6月24日 下午6:44:32
 */
@Component
public 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)

  1. package com.doctor.aop.aspect.demo;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. import com.doctor.aop.demo.Demo;  
  6.   
  7. /** 
  8.  * @description 
  9.  * 
  10.  * @author sdcuike 
  11.  * 
  12.  * @date 2016年6月27日 下午2:16:57 
  13.  *  
  14.  *       aspect 编译时候注入,克服动态代理方法嵌套调用失效问题. 
  15.  *  
  16.  *       运行方法:把项目转换成AspectJ工程,运行的时候选择:Run As-> AspectJ/Java Application 
  17.  */  
  18. class AspectAopDemoTest {  
  19.   
  20.     public static void main(String[] args) {  
  21.         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(  
  22.                 "classpath:/aopDemo1/spring-aop2.xml");  
  23.         Demo demo = applicationContext.getBean(Demo.class);  
  24.         demo.test01();  
  25.         demo.test02("doctor who");  
  26.         System.out.println("注入到spring容器的类实例是代理类" + demo.getClass());  
  27.         // class com.doctor.aop.demo.Demo
    EnhancerBySpringCGLIB 
    e9a9050a  
  28.         // 注入到spring容器的类实例是代理类  
  29.   
  30.         applicationContext.close();  
  31.     }  
  32.   
  33. }  
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();
	}

}


输出结果:

  1. 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 hierarchy  
  2. 07-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]  
  3. AspectAopDemo===before  execution(void com.doctor.aop.demo.Demo.test01())  
  4. test1  
  5. this对象指的不是代理类对象,而是原对象class com.doctor.aop.demo.Demo  
  6. AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))  
  7. AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))  
  8. AspectAopDemo===after   execution(void com.doctor.aop.demo.Demo.test01())  
  9. AspectAopDemo===before  execution(String com.doctor.aop.demo.Demo.test02(String))  
  10. AspectAopDemo===after   execution(String com.doctor.aop.demo.Demo.test02(String))  
  11. 注入到spring容器的类实例是代理类class com.doctor.aop.demo.Demo  
  12. 07-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  
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 hierarchy
07-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())
test1
this对象指的不是代理类对象,而是原对象class com.doctor.aop.demo.Demo
AspectAopDemo===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.Demo
07-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之上,它有可能将通知应用于一个已配置的通知之上。对于一个你没有注意到这方面问题的大范围应用的通知,这有可能导致一个无限循环。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值