11/* 
2-  * Copyright 2002-2013  the original author or authors. 
2+  * Copyright 2002-2014  the original author or authors. 
33 * 
44 * Licensed under the Apache License, Version 2.0 (the "License"); 
55 * you may not use this file except in compliance with the License. 
1616
1717package  org .springframework .aop .framework .autoproxy ;
1818
19+ import  java .io .Serializable ;
1920import  java .lang .reflect .Proxy ;
2021
2122import  org .aopalliance .intercept .MethodInterceptor ;
2425
2526import  org .springframework .aop .TargetSource ;
2627import  org .springframework .aop .support .AopUtils ;
27- import  org .springframework .tests .sample .beans .ITestBean ;
28- import  org .springframework .tests .sample .beans .IndexedTestBean ;
2928import  org .springframework .beans .MutablePropertyValues ;
30- import  org .springframework .tests .sample .beans .TestBean ;
31- import  org .springframework .tests .sample .beans .factory .DummyFactory ;
29+ import  org .springframework .beans .factory .BeanFactory ;
30+ import  org .springframework .beans .factory .BeanFactoryAware ;
31+ import  org .springframework .beans .factory .DisposableBean ;
3232import  org .springframework .beans .factory .FactoryBean ;
33+ import  org .springframework .beans .factory .InitializingBean ;
3334import  org .springframework .beans .factory .config .BeanDefinitionHolder ;
3435import  org .springframework .beans .factory .support .RootBeanDefinition ;
36+ import  org .springframework .context .ApplicationContext ;
37+ import  org .springframework .context .ApplicationContextAware ;
3538import  org .springframework .context .MessageSource ;
3639import  org .springframework .context .support .StaticApplicationContext ;
3740import  org .springframework .context .support .StaticMessageSource ;
41+ import  org .springframework .tests .sample .beans .ITestBean ;
42+ import  org .springframework .tests .sample .beans .IndexedTestBean ;
43+ import  org .springframework .tests .sample .beans .TestBean ;
44+ import  org .springframework .tests .sample .beans .factory .DummyFactory ;
3845
3946import  static  org .junit .Assert .*;
4047
@@ -133,16 +140,23 @@ public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
133140	public  void  testCustomAutoProxyCreator () {
134141		StaticApplicationContext  sac  = new  StaticApplicationContext ();
135142		sac .registerSingleton ("testAutoProxyCreator" , TestAutoProxyCreator .class );
143+ 		sac .registerSingleton ("noInterfaces" , NoInterfaces .class );
144+ 		sac .registerSingleton ("containerCallbackInterfacesOnly" , ContainerCallbackInterfacesOnly .class );
136145		sac .registerSingleton ("singletonNoInterceptor" , TestBean .class );
137146		sac .registerSingleton ("singletonToBeProxied" , TestBean .class );
138147		sac .registerPrototype ("prototypeToBeProxied" , TestBean .class );
139148		sac .refresh ();
140149
141150		MessageSource  messageSource  = (MessageSource ) sac .getBean ("messageSource" );
151+ 		NoInterfaces  noInterfaces  = (NoInterfaces ) sac .getBean ("noInterfaces" );
152+ 		ContainerCallbackInterfacesOnly  containerCallbackInterfacesOnly  =
153+ 				(ContainerCallbackInterfacesOnly ) sac .getBean ("containerCallbackInterfacesOnly" );
142154		ITestBean  singletonNoInterceptor  = (ITestBean ) sac .getBean ("singletonNoInterceptor" );
143155		ITestBean  singletonToBeProxied  = (ITestBean ) sac .getBean ("singletonToBeProxied" );
144156		ITestBean  prototypeToBeProxied  = (ITestBean ) sac .getBean ("prototypeToBeProxied" );
145157		assertFalse (AopUtils .isCglibProxy (messageSource ));
158+ 		assertTrue (AopUtils .isCglibProxy (noInterfaces ));
159+ 		assertTrue (AopUtils .isCglibProxy (containerCallbackInterfacesOnly ));
146160		assertTrue (AopUtils .isCglibProxy (singletonNoInterceptor ));
147161		assertTrue (AopUtils .isCglibProxy (singletonToBeProxied ));
148162		assertTrue (AopUtils .isCglibProxy (prototypeToBeProxied ));
@@ -157,6 +171,41 @@ public void testCustomAutoProxyCreator() {
157171		assertEquals (2 , tapc .testInterceptor .nrOfInvocations );
158172	}
159173
174+ 	@ Test 
175+ 	public  void  testAutoProxyCreatorWithFallbackToTargetClass () {
176+ 		StaticApplicationContext  sac  = new  StaticApplicationContext ();
177+ 		sac .registerSingleton ("testAutoProxyCreator" , FallbackTestAutoProxyCreator .class );
178+ 		sac .registerSingleton ("noInterfaces" , NoInterfaces .class );
179+ 		sac .registerSingleton ("containerCallbackInterfacesOnly" , ContainerCallbackInterfacesOnly .class );
180+ 		sac .registerSingleton ("singletonNoInterceptor" , TestBean .class );
181+ 		sac .registerSingleton ("singletonToBeProxied" , TestBean .class );
182+ 		sac .registerPrototype ("prototypeToBeProxied" , TestBean .class );
183+ 		sac .refresh ();
184+ 
185+ 		MessageSource  messageSource  = (MessageSource ) sac .getBean ("messageSource" );
186+ 		NoInterfaces  noInterfaces  = (NoInterfaces ) sac .getBean ("noInterfaces" );
187+ 		ContainerCallbackInterfacesOnly  containerCallbackInterfacesOnly  =
188+ 				(ContainerCallbackInterfacesOnly ) sac .getBean ("containerCallbackInterfacesOnly" );
189+ 		ITestBean  singletonNoInterceptor  = (ITestBean ) sac .getBean ("singletonNoInterceptor" );
190+ 		ITestBean  singletonToBeProxied  = (ITestBean ) sac .getBean ("singletonToBeProxied" );
191+ 		ITestBean  prototypeToBeProxied  = (ITestBean ) sac .getBean ("prototypeToBeProxied" );
192+ 		assertFalse (AopUtils .isCglibProxy (messageSource ));
193+ 		assertTrue (AopUtils .isCglibProxy (noInterfaces ));
194+ 		assertTrue (AopUtils .isCglibProxy (containerCallbackInterfacesOnly ));
195+ 		assertFalse (AopUtils .isCglibProxy (singletonNoInterceptor ));
196+ 		assertFalse (AopUtils .isCglibProxy (singletonToBeProxied ));
197+ 		assertFalse (AopUtils .isCglibProxy (prototypeToBeProxied ));
198+ 
199+ 		TestAutoProxyCreator  tapc  = (TestAutoProxyCreator ) sac .getBean ("testAutoProxyCreator" );
200+ 		assertEquals (0 , tapc .testInterceptor .nrOfInvocations );
201+ 		singletonNoInterceptor .getName ();
202+ 		assertEquals (0 , tapc .testInterceptor .nrOfInvocations );
203+ 		singletonToBeProxied .getAge ();
204+ 		assertEquals (1 , tapc .testInterceptor .nrOfInvocations );
205+ 		prototypeToBeProxied .getSpouse ();
206+ 		assertEquals (2 , tapc .testInterceptor .nrOfInvocations );
207+ 	}
208+ 
160209	@ Test 
161210	public  void  testAutoProxyCreatorWithFactoryBean () {
162211		StaticApplicationContext  sac  = new  StaticApplicationContext ();
@@ -303,6 +352,14 @@ else if (name.endsWith("ToBeProxied")) {
303352	}
304353
305354
355+ 	public  static  class  FallbackTestAutoProxyCreator  extends  TestAutoProxyCreator  {
356+ 
357+ 		public  FallbackTestAutoProxyCreator () {
358+ 			setProxyTargetClass (false );
359+ 		}
360+ 	}
361+ 
362+ 
306363	/** 
307364	 * Interceptor that counts the number of non-finalize method calls. 
308365	 */ 
@@ -319,4 +376,29 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
319376		}
320377	}
321378
379+ 
380+ 	public  static  class  NoInterfaces  {
381+ 	}
382+ 
383+ 
384+ 	public  static  class  ContainerCallbackInterfacesOnly   // as well as an empty marker interface 
385+ 			implements  BeanFactoryAware , ApplicationContextAware , InitializingBean , DisposableBean , Serializable  {
386+ 
387+ 		@ Override 
388+ 		public  void  setBeanFactory (BeanFactory  beanFactory ) {
389+ 		}
390+ 
391+ 		@ Override 
392+ 		public  void  setApplicationContext (ApplicationContext  applicationContext ) {
393+ 		}
394+ 
395+ 		@ Override 
396+ 		public  void  afterPropertiesSet () {
397+ 		}
398+ 
399+ 		@ Override 
400+ 		public  void  destroy () {
401+ 		}
402+ 	}
403+ 
322404}
0 commit comments