2121import java .io .ObjectInputStream ;
2222import java .io .ObjectStreamException ;
2323import java .io .Serializable ;
24+
2425import java .lang .annotation .Annotation ;
2526import java .lang .ref .Reference ;
2627import java .lang .ref .WeakReference ;
27- import java .lang .reflect .ParameterizedType ;
28- import java .lang .reflect .Type ;
28+
2929import java .security .AccessController ;
3030import java .security .PrivilegedAction ;
31+
3132import java .util .ArrayList ;
3233import java .util .Arrays ;
3334import java .util .Collection ;
3839import java .util .Map ;
3940import java .util .Set ;
4041import java .util .concurrent .ConcurrentHashMap ;
42+
4143import javax .inject .Provider ;
4244
4345import org .springframework .beans .BeansException ;
9092 * @author Juergen Hoeller
9193 * @author Sam Brannen
9294 * @author Costin Leau
95+ * @author Chris Beams
9396 * @since 16 April 2001
9497 * @see StaticListableBeanFactory
9598 * @see PropertiesBeanDefinitionReader
98101public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
99102implements ConfigurableListableBeanFactory , BeanDefinitionRegistry , Serializable {
100103
101- private static Class javaxInjectProviderClass = null ;
104+ private static Class <?> javaxInjectProviderClass = null ;
102105
103106static {
104107ClassLoader cl = DefaultListableBeanFactory .class .getClassLoader ();
@@ -128,11 +131,17 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
128131private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver ();
129132
130133/** 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 >();
132135
133136/** Map of bean definition objects, keyed by bean name */
134137private final Map <String , BeanDefinition > beanDefinitionMap = new ConcurrentHashMap <String , BeanDefinition >();
135138
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+
136145/** List of bean definition names, in registration order */
137146private final List <String > beanDefinitionNames = new ArrayList <String >();
138147
@@ -294,11 +303,26 @@ public String[] getBeanDefinitionNames() {
294303}
295304}
296305
297- public String [] getBeanNamesForType (Class type ) {
306+ public String [] getBeanNamesForType (Class <?> type ) {
298307return getBeanNamesForType (type , true , true );
299308}
300309
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 ) {
302326List <String > result = new ArrayList <String >();
303327
304328// Check all bean definitions.
@@ -441,7 +465,7 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
441465 */
442466public <A extends Annotation > A findAnnotationOnBean (String beanName , Class <A > annotationType ) {
443467A ann = null ;
444- Class beanType = getType (beanName );
468+ Class <?> beanType = getType (beanName );
445469if (beanType != null ) {
446470ann = AnnotationUtils .findAnnotation (beanType , annotationType );
447471}
@@ -564,18 +588,18 @@ public void preInstantiateSingletons() throws BeansException {
564588RootBeanDefinition bd = getMergedLocalBeanDefinition (beanName );
565589if (!bd .isAbstract () && bd .isSingleton () && !bd .isLazyInit ()) {
566590if (isFactoryBean (beanName )) {
567- final FactoryBean factory = (FactoryBean ) getBean (FACTORY_BEAN_PREFIX + beanName );
591+ final FactoryBean <?> factory = (FactoryBean <?> ) getBean (FACTORY_BEAN_PREFIX + beanName );
568592boolean isEagerInit ;
569593if (System .getSecurityManager () != null && factory instanceof SmartFactoryBean ) {
570594isEagerInit = AccessController .doPrivileged (new PrivilegedAction <Boolean >() {
571595public Boolean run () {
572- return ((SmartFactoryBean ) factory ).isEagerInit ();
596+ return ((SmartFactoryBean <?> ) factory ).isEagerInit ();
573597}
574598}, getAccessControlContext ());
575599}
576600else {
577601isEagerInit = (factory instanceof SmartFactoryBean &&
578- ((SmartFactoryBean ) factory ).isEagerInit ());
602+ ((SmartFactoryBean <?> ) factory ).isEagerInit ());
579603}
580604if (isEagerInit ) {
581605getBean (beanName );
@@ -669,6 +693,10 @@ protected void resetBeanDefinition(String beanName) {
669693destroySingleton (beanName );
670694}
671695
696+ // Remove any assumptions about by-type mappings
697+ this .singletonBeanNamesByType .clear ();
698+ this .nonSingletonBeanNamesByType .clear ();
699+
672700// Reset all bean definitions that have the given bean as parent (recursively).
673701for (String bdName : this .beanDefinitionNames ) {
674702if (!beanName .equals (bdName )) {
@@ -723,7 +751,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
723751}
724752
725753if (type .isArray ()) {
726- Class componentType = type .getComponentType ();
754+ Class <?> componentType = type .getComponentType ();
727755Map <String , Object > matchingBeans = findAutowireCandidates (beanName , componentType , descriptor );
728756if (matchingBeans .isEmpty ()) {
729757if (descriptor .isRequired ()) {
@@ -738,7 +766,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
738766return converter .convertIfNecessary (matchingBeans .values (), type );
739767}
740768else if (Collection .class .isAssignableFrom (type ) && type .isInterface ()) {
741- Class elementType = descriptor .getCollectionType ();
769+ Class <?> elementType = descriptor .getCollectionType ();
742770if (elementType == null ) {
743771if (descriptor .isRequired ()) {
744772throw new FatalBeanException ("No element type declared for collection [" + type .getName () + "]" );
@@ -759,15 +787,15 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
759787return converter .convertIfNecessary (matchingBeans .values (), type );
760788}
761789else if (Map .class .isAssignableFrom (type ) && type .isInterface ()) {
762- Class keyType = descriptor .getMapKeyType ();
790+ Class <?> keyType = descriptor .getMapKeyType ();
763791if (keyType == null || !String .class .isAssignableFrom (keyType )) {
764792if (descriptor .isRequired ()) {
765793throw new FatalBeanException ("Key type [" + keyType + "] of map [" + type .getName () +
766794"] must be assignable to [java.lang.String]" );
767795}
768796return null ;
769797}
770- Class valueType = descriptor .getMapValueType ();
798+ Class <?> valueType = descriptor .getMapValueType ();
771799if (valueType == null ) {
772800if (descriptor .isRequired ()) {
773801throw new FatalBeanException ("No value type declared for map [" + type .getName () + "]" );
@@ -828,12 +856,12 @@ else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
828856 * @see #autowireConstructor
829857 */
830858protected Map <String , Object > findAutowireCandidates (
831- String beanName , Class requiredType , DependencyDescriptor descriptor ) {
859+ String beanName , Class <?> requiredType , DependencyDescriptor descriptor ) {
832860
833861String [] candidateNames = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
834862this , requiredType , true , descriptor .isEager ());
835863Map <String , Object > result = new LinkedHashMap <String , Object >(candidateNames .length );
836- for (Class autowiringType : this .resolvableDependencies .keySet ()) {
864+ for (Class <?> autowiringType : this .resolvableDependencies .keySet ()) {
837865if (autowiringType .isAssignableFrom (requiredType )) {
838866Object autowiringValue = this .resolvableDependencies .get (autowiringType );
839867autowiringValue = AutowireUtils .resolveAutowiringValue (autowiringValue , requiredType );
@@ -918,7 +946,7 @@ protected boolean matchesBeanName(String beanName, String candidateName) {
918946 * Raise a NoSuchBeanDefinitionException for an unresolvable dependency.
919947 */
920948private void raiseNoSuchBeanDefinitionException (
921- Class type , String dependencyDescription , DependencyDescriptor descriptor )
949+ Class <?> type , String dependencyDescription , DependencyDescriptor descriptor )
922950throws NoSuchBeanDefinitionException {
923951
924952throw new NoSuchBeanDefinitionException (type , dependencyDescription ,
@@ -967,6 +995,7 @@ protected Object writeReplace() throws ObjectStreamException {
967995 * Minimal id reference to the factory.
968996 * Resolved to the actual factory instance on deserialization.
969997 */
998+ @ SuppressWarnings ("serial" )
970999private static class SerializedBeanFactoryReference implements Serializable {
9711000
9721001private final String id ;
@@ -976,7 +1005,7 @@ public SerializedBeanFactoryReference(String id) {
9761005}
9771006
9781007private Object readResolve () {
979- Reference ref = serializableFactories .get (this .id );
1008+ Reference <?> ref = serializableFactories .get (this .id );
9801009if (ref == null ) {
9811010throw new IllegalStateException (
9821011"Cannot deserialize BeanFactory with id " + this .id + ": no factory registered for this id" );
@@ -994,7 +1023,8 @@ private Object readResolve() {
9941023/**
9951024 * Serializable ObjectFactory for lazy resolution of a dependency.
9961025 */
997- private class DependencyObjectFactory implements ObjectFactory , Serializable {
1026+ @ SuppressWarnings ("serial" )
1027+ private class DependencyObjectFactory implements ObjectFactory <Object >, Serializable {
9981028
9991029private final DependencyDescriptor descriptor ;
10001030
@@ -1015,7 +1045,8 @@ public Object getObject() throws BeansException {
10151045/**
10161046 * Serializable ObjectFactory for lazy resolution of a dependency.
10171047 */
1018- private class DependencyProvider extends DependencyObjectFactory implements Provider {
1048+ @ SuppressWarnings ("serial" )
1049+ private class DependencyProvider extends DependencyObjectFactory implements Provider <Object > {
10191050
10201051public DependencyProvider (DependencyDescriptor descriptor , String beanName ) {
10211052super (descriptor , beanName );
0 commit comments