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 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
*/
@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;
}
}
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();
}
}
看一下输出:
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。
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
*/
@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)
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 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
使用AspectJ的一个间接局限是,因为AspectJ通知可以应用于POJO之上,它有可能将通知应用于一个已配置的通知之上。对于一个你没有注意到这方面问题的大范围应用的通知,这有可能导致一个无限循环。