1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .aop .framework .autoproxy ;
18
18
19
+ import java .io .Serializable ;
19
20
import java .lang .reflect .Proxy ;
20
21
21
22
import org .aopalliance .intercept .MethodInterceptor ;
24
25
25
26
import org .springframework .aop .TargetSource ;
26
27
import org .springframework .aop .support .AopUtils ;
27
- import org .springframework .tests .sample .beans .ITestBean ;
28
- import org .springframework .tests .sample .beans .IndexedTestBean ;
29
28
import 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 ;
32
32
import org .springframework .beans .factory .FactoryBean ;
33
+ import org .springframework .beans .factory .InitializingBean ;
33
34
import org .springframework .beans .factory .config .BeanDefinitionHolder ;
34
35
import org .springframework .beans .factory .support .RootBeanDefinition ;
36
+ import org .springframework .context .ApplicationContext ;
37
+ import org .springframework .context .ApplicationContextAware ;
35
38
import org .springframework .context .MessageSource ;
36
39
import org .springframework .context .support .StaticApplicationContext ;
37
40
import 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 ;
38
45
39
46
import static org .junit .Assert .*;
40
47
@@ -133,16 +140,23 @@ public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
133
140
public void testCustomAutoProxyCreator () {
134
141
StaticApplicationContext sac = new StaticApplicationContext ();
135
142
sac .registerSingleton ("testAutoProxyCreator" , TestAutoProxyCreator .class );
143
+ sac .registerSingleton ("noInterfaces" , NoInterfaces .class );
144
+ sac .registerSingleton ("containerCallbackInterfacesOnly" , ContainerCallbackInterfacesOnly .class );
136
145
sac .registerSingleton ("singletonNoInterceptor" , TestBean .class );
137
146
sac .registerSingleton ("singletonToBeProxied" , TestBean .class );
138
147
sac .registerPrototype ("prototypeToBeProxied" , TestBean .class );
139
148
sac .refresh ();
140
149
141
150
MessageSource messageSource = (MessageSource ) sac .getBean ("messageSource" );
151
+ NoInterfaces noInterfaces = (NoInterfaces ) sac .getBean ("noInterfaces" );
152
+ ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
153
+ (ContainerCallbackInterfacesOnly ) sac .getBean ("containerCallbackInterfacesOnly" );
142
154
ITestBean singletonNoInterceptor = (ITestBean ) sac .getBean ("singletonNoInterceptor" );
143
155
ITestBean singletonToBeProxied = (ITestBean ) sac .getBean ("singletonToBeProxied" );
144
156
ITestBean prototypeToBeProxied = (ITestBean ) sac .getBean ("prototypeToBeProxied" );
145
157
assertFalse (AopUtils .isCglibProxy (messageSource ));
158
+ assertTrue (AopUtils .isCglibProxy (noInterfaces ));
159
+ assertTrue (AopUtils .isCglibProxy (containerCallbackInterfacesOnly ));
146
160
assertTrue (AopUtils .isCglibProxy (singletonNoInterceptor ));
147
161
assertTrue (AopUtils .isCglibProxy (singletonToBeProxied ));
148
162
assertTrue (AopUtils .isCglibProxy (prototypeToBeProxied ));
@@ -157,6 +171,41 @@ public void testCustomAutoProxyCreator() {
157
171
assertEquals (2 , tapc .testInterceptor .nrOfInvocations );
158
172
}
159
173
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
+
160
209
@ Test
161
210
public void testAutoProxyCreatorWithFactoryBean () {
162
211
StaticApplicationContext sac = new StaticApplicationContext ();
@@ -303,6 +352,14 @@ else if (name.endsWith("ToBeProxied")) {
303
352
}
304
353
305
354
355
+ public static class FallbackTestAutoProxyCreator extends TestAutoProxyCreator {
356
+
357
+ public FallbackTestAutoProxyCreator () {
358
+ setProxyTargetClass (false );
359
+ }
360
+ }
361
+
362
+
306
363
/**
307
364
* Interceptor that counts the number of non-finalize method calls.
308
365
*/
@@ -319,4 +376,29 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
319
376
}
320
377
}
321
378
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
+
322
404
}
0 commit comments