21
21
import java .io .ObjectInputStream ;
22
22
import java .io .ObjectStreamException ;
23
23
import java .io .Serializable ;
24
+
24
25
import java .lang .annotation .Annotation ;
25
26
import java .lang .ref .Reference ;
26
27
import java .lang .ref .WeakReference ;
27
- import java .lang .reflect .ParameterizedType ;
28
- import java .lang .reflect .Type ;
28
+
29
29
import java .security .AccessController ;
30
30
import java .security .PrivilegedAction ;
31
+
31
32
import java .util .ArrayList ;
32
33
import java .util .Arrays ;
33
34
import java .util .Collection ;
38
39
import java .util .Map ;
39
40
import java .util .Set ;
40
41
import java .util .concurrent .ConcurrentHashMap ;
42
+
41
43
import javax .inject .Provider ;
42
44
43
45
import org .springframework .beans .BeansException ;
90
92
* @author Juergen Hoeller
91
93
* @author Sam Brannen
92
94
* @author Costin Leau
95
+ * @author Chris Beams
93
96
* @since 16 April 2001
94
97
* @see StaticListableBeanFactory
95
98
* @see PropertiesBeanDefinitionReader
98
101
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
99
102
implements ConfigurableListableBeanFactory , BeanDefinitionRegistry , Serializable {
100
103
101
- private static Class javaxInjectProviderClass = null ;
104
+ private static Class <?> javaxInjectProviderClass = null ;
102
105
103
106
static {
104
107
ClassLoader cl = DefaultListableBeanFactory .class .getClassLoader ();
@@ -128,11 +131,17 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
128
131
private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver ();
129
132
130
133
/** Map from dependency type to corresponding autowired value */
131
- private final Map <Class , Object > resolvableDependencies = new HashMap <Class , Object >();
134
+ private final Map <Class <?> , Object > resolvableDependencies = new HashMap <Class <?> , Object >();
132
135
133
136
/** Map of bean definition objects, keyed by bean name */
134
137
private final Map <String , BeanDefinition > beanDefinitionMap = new ConcurrentHashMap <String , BeanDefinition >();
135
138
139
+ /** Map of singleton bean names keyed by bean class */
140
+ private final Map <Class <?>, String []> singletonBeanNamesByType = new ConcurrentHashMap <Class <?>, String []>();
141
+
142
+ /** Map of non-singleton bean names keyed by bean class */
143
+ private final Map <Class <?>, String []> nonSingletonBeanNamesByType = new ConcurrentHashMap <Class <?>, String []>();
144
+
136
145
/** List of bean definition names, in registration order */
137
146
private final List <String > beanDefinitionNames = new ArrayList <String >();
138
147
@@ -294,11 +303,26 @@ public String[] getBeanDefinitionNames() {
294
303
}
295
304
}
296
305
297
- public String [] getBeanNamesForType (Class type ) {
306
+ public String [] getBeanNamesForType (Class <?> type ) {
298
307
return getBeanNamesForType (type , true , true );
299
308
}
300
309
301
- public String [] getBeanNamesForType (Class type , boolean includeNonSingletons , boolean allowEagerInit ) {
310
+ public String [] getBeanNamesForType (Class <?> type , boolean includeNonSingletons , boolean allowEagerInit ) {
311
+ if (type == null || !allowEagerInit ) {
312
+ return this .doGetBeanNamesForType (type , includeNonSingletons , allowEagerInit );
313
+ }
314
+ Map <Class <?>, String []> cache = includeNonSingletons ?
315
+ this .nonSingletonBeanNamesByType : this .singletonBeanNamesByType ;
316
+ String [] resolvedBeanNames = cache .get (type );
317
+ if (resolvedBeanNames != null ) {
318
+ return resolvedBeanNames ;
319
+ }
320
+ resolvedBeanNames = this .doGetBeanNamesForType (type , includeNonSingletons , allowEagerInit );
321
+ cache .put (type , resolvedBeanNames );
322
+ return resolvedBeanNames ;
323
+ }
324
+
325
+ private String [] doGetBeanNamesForType (Class <?> type , boolean includeNonSingletons , boolean allowEagerInit ) {
302
326
List <String > result = new ArrayList <String >();
303
327
304
328
// Check all bean definitions.
@@ -441,7 +465,7 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
441
465
*/
442
466
public <A extends Annotation > A findAnnotationOnBean (String beanName , Class <A > annotationType ) {
443
467
A ann = null ;
444
- Class beanType = getType (beanName );
468
+ Class <?> beanType = getType (beanName );
445
469
if (beanType != null ) {
446
470
ann = AnnotationUtils .findAnnotation (beanType , annotationType );
447
471
}
@@ -564,18 +588,18 @@ public void preInstantiateSingletons() throws BeansException {
564
588
RootBeanDefinition bd = getMergedLocalBeanDefinition (beanName );
565
589
if (!bd .isAbstract () && bd .isSingleton () && !bd .isLazyInit ()) {
566
590
if (isFactoryBean (beanName )) {
567
- final FactoryBean factory = (FactoryBean ) getBean (FACTORY_BEAN_PREFIX + beanName );
591
+ final FactoryBean <?> factory = (FactoryBean <?> ) getBean (FACTORY_BEAN_PREFIX + beanName );
568
592
boolean isEagerInit ;
569
593
if (System .getSecurityManager () != null && factory instanceof SmartFactoryBean ) {
570
594
isEagerInit = AccessController .doPrivileged (new PrivilegedAction <Boolean >() {
571
595
public Boolean run () {
572
- return ((SmartFactoryBean ) factory ).isEagerInit ();
596
+ return ((SmartFactoryBean <?> ) factory ).isEagerInit ();
573
597
}
574
598
}, getAccessControlContext ());
575
599
}
576
600
else {
577
601
isEagerInit = (factory instanceof SmartFactoryBean &&
578
- ((SmartFactoryBean ) factory ).isEagerInit ());
602
+ ((SmartFactoryBean <?> ) factory ).isEagerInit ());
579
603
}
580
604
if (isEagerInit ) {
581
605
getBean (beanName );
@@ -669,6 +693,10 @@ protected void resetBeanDefinition(String beanName) {
669
693
destroySingleton (beanName );
670
694
}
671
695
696
+ // Remove any assumptions about by-type mappings
697
+ this .singletonBeanNamesByType .clear ();
698
+ this .nonSingletonBeanNamesByType .clear ();
699
+
672
700
// Reset all bean definitions that have the given bean as parent (recursively).
673
701
for (String bdName : this .beanDefinitionNames ) {
674
702
if (!beanName .equals (bdName )) {
@@ -723,7 +751,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
723
751
}
724
752
725
753
if (type .isArray ()) {
726
- Class componentType = type .getComponentType ();
754
+ Class <?> componentType = type .getComponentType ();
727
755
Map <String , Object > matchingBeans = findAutowireCandidates (beanName , componentType , descriptor );
728
756
if (matchingBeans .isEmpty ()) {
729
757
if (descriptor .isRequired ()) {
@@ -738,7 +766,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
738
766
return converter .convertIfNecessary (matchingBeans .values (), type );
739
767
}
740
768
else if (Collection .class .isAssignableFrom (type ) && type .isInterface ()) {
741
- Class elementType = descriptor .getCollectionType ();
769
+ Class <?> elementType = descriptor .getCollectionType ();
742
770
if (elementType == null ) {
743
771
if (descriptor .isRequired ()) {
744
772
throw new FatalBeanException ("No element type declared for collection [" + type .getName () + "]" );
@@ -759,15 +787,15 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
759
787
return converter .convertIfNecessary (matchingBeans .values (), type );
760
788
}
761
789
else if (Map .class .isAssignableFrom (type ) && type .isInterface ()) {
762
- Class keyType = descriptor .getMapKeyType ();
790
+ Class <?> keyType = descriptor .getMapKeyType ();
763
791
if (keyType == null || !String .class .isAssignableFrom (keyType )) {
764
792
if (descriptor .isRequired ()) {
765
793
throw new FatalBeanException ("Key type [" + keyType + "] of map [" + type .getName () +
766
794
"] must be assignable to [java.lang.String]" );
767
795
}
768
796
return null ;
769
797
}
770
- Class valueType = descriptor .getMapValueType ();
798
+ Class <?> valueType = descriptor .getMapValueType ();
771
799
if (valueType == null ) {
772
800
if (descriptor .isRequired ()) {
773
801
throw new FatalBeanException ("No value type declared for map [" + type .getName () + "]" );
@@ -828,12 +856,12 @@ else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
828
856
* @see #autowireConstructor
829
857
*/
830
858
protected Map <String , Object > findAutowireCandidates (
831
- String beanName , Class requiredType , DependencyDescriptor descriptor ) {
859
+ String beanName , Class <?> requiredType , DependencyDescriptor descriptor ) {
832
860
833
861
String [] candidateNames = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
834
862
this , requiredType , true , descriptor .isEager ());
835
863
Map <String , Object > result = new LinkedHashMap <String , Object >(candidateNames .length );
836
- for (Class autowiringType : this .resolvableDependencies .keySet ()) {
864
+ for (Class <?> autowiringType : this .resolvableDependencies .keySet ()) {
837
865
if (autowiringType .isAssignableFrom (requiredType )) {
838
866
Object autowiringValue = this .resolvableDependencies .get (autowiringType );
839
867
autowiringValue = AutowireUtils .resolveAutowiringValue (autowiringValue , requiredType );
@@ -918,7 +946,7 @@ protected boolean matchesBeanName(String beanName, String candidateName) {
918
946
* Raise a NoSuchBeanDefinitionException for an unresolvable dependency.
919
947
*/
920
948
private void raiseNoSuchBeanDefinitionException (
921
- Class type , String dependencyDescription , DependencyDescriptor descriptor )
949
+ Class <?> type , String dependencyDescription , DependencyDescriptor descriptor )
922
950
throws NoSuchBeanDefinitionException {
923
951
924
952
throw new NoSuchBeanDefinitionException (type , dependencyDescription ,
@@ -967,6 +995,7 @@ protected Object writeReplace() throws ObjectStreamException {
967
995
* Minimal id reference to the factory.
968
996
* Resolved to the actual factory instance on deserialization.
969
997
*/
998
+ @ SuppressWarnings ("serial" )
970
999
private static class SerializedBeanFactoryReference implements Serializable {
971
1000
972
1001
private final String id ;
@@ -976,7 +1005,7 @@ public SerializedBeanFactoryReference(String id) {
976
1005
}
977
1006
978
1007
private Object readResolve () {
979
- Reference ref = serializableFactories .get (this .id );
1008
+ Reference <?> ref = serializableFactories .get (this .id );
980
1009
if (ref == null ) {
981
1010
throw new IllegalStateException (
982
1011
"Cannot deserialize BeanFactory with id " + this .id + ": no factory registered for this id" );
@@ -994,7 +1023,8 @@ private Object readResolve() {
994
1023
/**
995
1024
* Serializable ObjectFactory for lazy resolution of a dependency.
996
1025
*/
997
- private class DependencyObjectFactory implements ObjectFactory , Serializable {
1026
+ @ SuppressWarnings ("serial" )
1027
+ private class DependencyObjectFactory implements ObjectFactory <Object >, Serializable {
998
1028
999
1029
private final DependencyDescriptor descriptor ;
1000
1030
@@ -1015,7 +1045,8 @@ public Object getObject() throws BeansException {
1015
1045
/**
1016
1046
* Serializable ObjectFactory for lazy resolution of a dependency.
1017
1047
*/
1018
- private class DependencyProvider extends DependencyObjectFactory implements Provider {
1048
+ @ SuppressWarnings ("serial" )
1049
+ private class DependencyProvider extends DependencyObjectFactory implements Provider <Object > {
1019
1050
1020
1051
public DependencyProvider (DependencyDescriptor descriptor , String beanName ) {
1021
1052
super (descriptor , beanName );
0 commit comments