1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
24
24
import java .util .Set ;
25
25
import javax .servlet .ServletContext ;
26
26
27
+ import org .springframework .beans .BeanUtils ;
27
28
import org .springframework .beans .BeansException ;
28
29
import org .springframework .beans .TypeConverter ;
29
30
import org .springframework .beans .factory .BeanFactory ;
42
43
import org .springframework .core .env .StandardEnvironment ;
43
44
import org .springframework .core .io .Resource ;
44
45
import org .springframework .core .io .support .ResourcePatternResolver ;
46
+ import org .springframework .util .ClassUtils ;
45
47
import org .springframework .util .ObjectUtils ;
46
48
import org .springframework .web .context .WebApplicationContext ;
47
49
import org .springframework .web .context .support .ServletContextResourcePatternResolver ;
48
50
49
51
/**
50
- * A mock WebApplicationContext that accepts registrations of object instances.
52
+ * A stub WebApplicationContext that accepts registrations of object instances.
51
53
*
52
- * <p>As registered object instances are instantiated and initialized
53
- * externally, there is no wiring, bean initialization, lifecycle events, as
54
- * well as no pre-processing and post-processing hooks typically associated with
55
- * beans managed by an {@link ApplicationContext}. Just a simple lookup into a
54
+ * <p>As registered object instances are instantiated and initialized externally,
55
+ * there is no wiring, bean initialization, lifecycle events, as well as no
56
+ * pre-processing and post-processing hooks typically associated with beans
57
+ * managed by an {@link ApplicationContext}. Just a simple lookup into a
56
58
* {@link StaticListableBeanFactory}.
57
59
*
58
60
* @author Rossen Stoyanchev
61
+ * @author Juergen Hoeller
59
62
* @since 3.2
60
63
*/
61
64
class StubWebApplicationContext implements WebApplicationContext {
@@ -77,14 +80,12 @@ class StubWebApplicationContext implements WebApplicationContext {
77
80
private final ResourcePatternResolver resourcePatternResolver ;
78
81
79
82
80
- /**
81
- * Class constructor.
82
- */
83
83
public StubWebApplicationContext (ServletContext servletContext ) {
84
84
this .servletContext = servletContext ;
85
85
this .resourcePatternResolver = new ServletContextResourcePatternResolver (servletContext );
86
86
}
87
87
88
+
88
89
/**
89
90
* Returns an instance that can initialize {@link ApplicationContextAware} beans.
90
91
*/
@@ -98,6 +99,7 @@ public ServletContext getServletContext() {
98
99
return this .servletContext ;
99
100
}
100
101
102
+
101
103
//---------------------------------------------------------------------
102
104
// Implementation of ApplicationContext interface
103
105
//---------------------------------------------------------------------
@@ -137,12 +139,16 @@ public void addBean(String name, Object bean) {
137
139
}
138
140
139
141
public void addBeans (List <?> beans ) {
142
+ if (beans == null ) {
143
+ return ;
144
+ }
140
145
for (Object bean : beans ) {
141
146
String name = bean .getClass ().getName () + "#" + ObjectUtils .getIdentityHexString (bean );
142
147
this .beanFactory .addBean (name , bean );
143
148
}
144
149
}
145
150
151
+
146
152
//---------------------------------------------------------------------
147
153
// Implementation of BeanFactory interface
148
154
//---------------------------------------------------------------------
@@ -202,6 +208,7 @@ public String[] getAliases(String name) {
202
208
return this .beanFactory .getAliases (name );
203
209
}
204
210
211
+
205
212
//---------------------------------------------------------------------
206
213
// Implementation of ListableBeanFactory interface
207
214
//---------------------------------------------------------------------
@@ -262,6 +269,7 @@ public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> a
262
269
return this .beanFactory .findAnnotationOnBean (beanName , annotationType );
263
270
}
264
271
272
+
265
273
//---------------------------------------------------------------------
266
274
// Implementation of HierarchicalBeanFactory interface
267
275
//---------------------------------------------------------------------
@@ -276,6 +284,7 @@ public boolean containsLocalBean(String name) {
276
284
return this .beanFactory .containsBean (name );
277
285
}
278
286
287
+
279
288
//---------------------------------------------------------------------
280
289
// Implementation of MessageSource interface
281
290
//---------------------------------------------------------------------
@@ -295,20 +304,22 @@ public String getMessage(MessageSourceResolvable resolvable, Locale locale) thro
295
304
return this .messageSource .getMessage (resolvable , locale );
296
305
}
297
306
307
+
298
308
//---------------------------------------------------------------------
299
309
// Implementation of ResourceLoader interface
300
310
//---------------------------------------------------------------------
301
311
302
312
@ Override
303
313
public ClassLoader getClassLoader () {
304
- return null ;
314
+ return ClassUtils . getDefaultClassLoader () ;
305
315
}
306
316
307
317
@ Override
308
318
public Resource getResource (String location ) {
309
319
return this .resourcePatternResolver .getResource (location );
310
320
}
311
321
322
+
312
323
//---------------------------------------------------------------------
313
324
// Other
314
325
//---------------------------------------------------------------------
@@ -340,65 +351,61 @@ public Object initializeBean(Object existingBean, String beanName) throws BeansE
340
351
341
352
@ Override
342
353
public <T > T createBean (Class <T > beanClass ) {
343
- throw new UnsupportedOperationException ( );
354
+ return BeanUtils . instantiate ( beanClass );
344
355
}
345
356
346
357
@ Override
347
358
@ SuppressWarnings ("rawtypes" )
348
- public Object createBean (Class beanClass , int autowireMode , boolean dependencyCheck ) {
349
- throw new UnsupportedOperationException ( );
359
+ public Object createBean (Class <?> beanClass , int autowireMode , boolean dependencyCheck ) {
360
+ return BeanUtils . instantiate ( beanClass );
350
361
}
351
362
352
363
@ Override
353
364
@ SuppressWarnings ("rawtypes" )
354
- public Object autowire (Class beanClass , int autowireMode , boolean dependencyCheck ) {
355
- throw new UnsupportedOperationException ( );
365
+ public Object autowire (Class <?> beanClass , int autowireMode , boolean dependencyCheck ) {
366
+ return BeanUtils . instantiate ( beanClass );
356
367
}
357
368
358
369
@ Override
359
370
public void autowireBean (Object existingBean ) throws BeansException {
360
- throw new UnsupportedOperationException ();
361
371
}
362
372
363
373
@ Override
364
374
public void autowireBeanProperties (Object existingBean , int autowireMode , boolean dependencyCheck ) {
365
- throw new UnsupportedOperationException ();
366
375
}
367
376
368
377
@ Override
369
378
public Object configureBean (Object existingBean , String beanName ) {
370
- throw new UnsupportedOperationException () ;
379
+ return existingBean ;
371
380
}
372
381
373
382
@ Override
374
383
public Object resolveDependency (DependencyDescriptor descriptor , String beanName ) {
375
- throw new UnsupportedOperationException ();
384
+ throw new UnsupportedOperationException ("Dependency resolution not supported" );
376
385
}
377
386
378
387
@ Override
379
388
public Object resolveDependency (DependencyDescriptor descriptor , String beanName ,
380
389
Set <String > autowiredBeanNames , TypeConverter typeConverter ) {
381
- throw new UnsupportedOperationException ();
390
+ throw new UnsupportedOperationException ("Dependency resolution not supported" );
382
391
}
383
392
384
393
@ Override
385
394
public void applyBeanPropertyValues (Object existingBean , String beanName ) throws BeansException {
386
- throw new UnsupportedOperationException ();
387
395
}
388
396
389
397
@ Override
390
398
public Object applyBeanPostProcessorsBeforeInitialization (Object existingBean , String beanName ) {
391
- throw new UnsupportedOperationException () ;
399
+ return existingBean ;
392
400
}
393
401
394
402
@ Override
395
403
public Object applyBeanPostProcessorsAfterInitialization (Object existingBean , String beanName ) {
396
- throw new UnsupportedOperationException () ;
404
+ return existingBean ;
397
405
}
398
406
399
407
@ Override
400
408
public void destroyBean (Object existingBean ) {
401
- throw new UnsupportedOperationException ();
402
409
}
403
410
}
404
411
0 commit comments