标签:
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
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; } }
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
标签:
原文地址:http://blog.csdn.net/doctor_who2004/article/details/51814476