org.springframework.beans.factory.support

Class DefaultListableBeanFactory

    • Constructor Detail

      • DefaultListableBeanFactory

        public DefaultListableBeanFactory()
        Create a new DefaultListableBeanFactory.
      • DefaultListableBeanFactory

        public DefaultListableBeanFactory(@Nullable
                                          BeanFactory parentBeanFactory)
        Create a new DefaultListableBeanFactory with the given parent.
        Parameters:
        parentBeanFactory - the parent BeanFactory
    • Method Detail

      • setSerializationId

        public void setSerializationId(@Nullable
                                       java.lang.String serializationId)
        Specify an id for serialization purposes, allowing this BeanFactory to be deserialized from this id back into the BeanFactory object, if needed.
      • getSerializationId

        @Nullable
        public java.lang.String getSerializationId()
        Return an id for serialization purposes, if specified, allowing this BeanFactory to be deserialized from this id back into the BeanFactory object, if needed.
        Since:
        4.1.2
      • isAllowBeanDefinitionOverriding

        public boolean isAllowBeanDefinitionOverriding()
        Return whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former.
        Since:
        4.1.2
      • setAllowEagerClassLoading

        public void setAllowEagerClassLoading(boolean allowEagerClassLoading)
        Set whether the factory is allowed to eagerly load bean classes even for bean definitions that are marked as "lazy-init".

        Default is "true". Turn this flag off to suppress class loading for lazy-init beans unless such a bean is explicitly requested. In particular, by-type lookups will then simply ignore bean definitions without resolved class name, instead of loading the bean classes on demand just to perform a type check.

        See Also:
        AbstractBeanDefinition.setLazyInit(boolean)
      • isAllowEagerClassLoading

        public boolean isAllowEagerClassLoading()
        Return whether the factory is allowed to eagerly load bean classes even for bean definitions that are marked as "lazy-init".
        Since:
        4.1.2
      • getDependencyComparator

        @Nullable
        public java.util.Comparator<java.lang.Object> getDependencyComparator()
        Return the dependency comparator for this BeanFactory (may be null.
        Since:
        4.0
      • setAutowireCandidateResolver

        public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver)
        Set a custom autowire candidate resolver for this BeanFactory to use when deciding whether a bean definition should be considered as a candidate for autowiring.
      • getAutowireCandidateResolver

        public AutowireCandidateResolver getAutowireCandidateResolver()
        Return the autowire candidate resolver for this BeanFactory (never null).
      • getBean

        public <T> T getBean(java.lang.Class<T> requiredType,
                             @Nullable
                             java.lang.Object... args)
                      throws BeansException
        Description copied from interface: BeanFactory
        Return an instance, which may be shared or independent, of the specified bean.

        Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.

        This method goes into ListableBeanFactory by-type lookup territory but may also be translated into a conventional by-name lookup based on the name of the given type. For more extensive retrieval operations across sets of beans, use ListableBeanFactory and/or BeanFactoryUtils.

        Specified by:
        getBean in interface BeanFactory
        Parameters:
        requiredType - type the bean must match; can be an interface or superclass
        args - arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)
        Returns:
        an instance of the bean
        Throws:
        NoSuchBeanDefinitionException - if there is no such bean definition
        BeanDefinitionStoreException - if arguments have been given but the affected bean isn't a prototype
        BeansException - if the bean could not be created
      • getBeanProvider

        public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType)
        Description copied from interface: BeanFactory
        Return an provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
        Specified by:
        getBeanProvider in interface BeanFactory
        Parameters:
        requiredType - type the bean must match; can be a generic type declaration. Note that collection types are not supported here, in contrast to reflective injection points. For programmatically retrieving a list of beans matching a specific type, specify the actual bean type as an argument here and subsequently use ObjectProvider#toList() or its lazy streaming/iteration options.
        Returns:
        a corresponding provider handle
        See Also:
        ObjectProvider.stream(), ObjectProvider.iterator(), ObjectProvider#toList()
      • getBeanDefinitionCount

        public int getBeanDefinitionCount()
        Description copied from interface: ListableBeanFactory
        Return the number of beans defined in the factory.

        Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.

        Specified by:
        getBeanDefinitionCount in interface ListableBeanFactory
        Specified by:
        getBeanDefinitionCount in interface BeanDefinitionRegistry
        Returns:
        the number of beans defined in the factory
      • getBeanDefinitionNames

        public java.lang.String[] getBeanDefinitionNames()
        Description copied from interface: ListableBeanFactory
        Return the names of all beans defined in this factory.

        Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.

        Specified by:
        getBeanDefinitionNames in interface ListableBeanFactory
        Specified by:
        getBeanDefinitionNames in interface BeanDefinitionRegistry
        Returns:
        the names of all beans defined in this factory, or an empty array if none defined
      • getBeanNamesForType

        public java.lang.String[] getBeanNamesForType(ResolvableType type)
        Description copied from interface: ListableBeanFactory
        Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

        NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

        Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

        Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors to include beans in ancestor factories too.

        Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

        This version of getBeanNamesForType matches all kinds of beans, be it singletons, prototypes, or FactoryBeans. In most implementations, the result will be the same as for getBeanNamesForType(type, true, true).

        Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

        Specified by:
        getBeanNamesForType in interface ListableBeanFactory
        Parameters:
        type - the generically typed class or interface to match
        Returns:
        the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
        See Also:
        BeanFactory.isTypeMatch(String, ResolvableType), FactoryBean.getObjectType(), BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)
      • getBeanNamesForType

        public java.lang.String[] getBeanNamesForType(@Nullable
                                                      java.lang.Class<?> type)
        Description copied from interface: ListableBeanFactory
        Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

        NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

        Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

        Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors to include beans in ancestor factories too.

        Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

        This version of getBeanNamesForType matches all kinds of beans, be it singletons, prototypes, or FactoryBeans. In most implementations, the result will be the same as for getBeanNamesForType(type, true, true).

        Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

        Specified by:
        getBeanNamesForType in interface ListableBeanFactory
        Parameters:
        type - the class or interface to match, or null for all bean names
        Returns:
        the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
        See Also:
        FactoryBean.getObjectType(), BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
      • getBeanNamesForType

        public java.lang.String[] getBeanNamesForType(@Nullable
                                                      java.lang.Class<?> type,
                                                      boolean includeNonSingletons,
                                                      boolean allowEagerInit)
        Description copied from interface: ListableBeanFactory
        Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

        NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

        Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

        Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors to include beans in ancestor factories too.

        Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

        Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

        Specified by:
        getBeanNamesForType in interface ListableBeanFactory
        Parameters:
        type - the class or interface to match, or null for all bean names
        includeNonSingletons - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
        allowEagerInit - whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
        Returns:
        the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
        See Also:
        FactoryBean.getObjectType(), BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
      • getBeansOfType

        public <T> java.util.Map<java.lang.String,T> getBeansOfType(@Nullable
                                                                    java.lang.Class<T> type)
                                                             throws BeansException
        Description copied from interface: ListableBeanFactory
        Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

        NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

        Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

        Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beansOfTypeIncludingAncestors to include beans in ancestor factories too.

        Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

        This version of getBeansOfType matches all kinds of beans, be it singletons, prototypes, or FactoryBeans. In most implementations, the result will be the same as for getBeansOfType(type, true, true).

        The Map returned by this method should always return bean names and corresponding bean instances in the order of definition in the backend configuration, as far as possible.

        Specified by:
        getBeansOfType in interface ListableBeanFactory
        Parameters:
        type - the class or interface to match, or null for all concrete beans
        Returns:
        a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
        Throws:
        BeansException - if a bean could not be created
        See Also:
        FactoryBean.getObjectType(), BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
      • getBeansOfType

        public <T> java.util.Map<java.lang.String,T> getBeansOfType(@Nullable
                                                                    java.lang.Class<T> type,
                                                                    boolean includeNonSingletons,
                                                                    boolean allowEagerInit)
                                                             throws BeansException
        Description copied from interface: ListableBeanFactory
        Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

        NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.

        Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

        Does not consider any hierarchy this factory may participate in. Use BeanFactoryUtils' beansOfTypeIncludingAncestors to include beans in ancestor factories too.

        Note: Does not ignore singleton beans that have been registered by other means than bean definitions.

        The Map returned by this method should always return bean names and corresponding bean instances in the order of definition in the backend configuration, as far as possible.

        Specified by:
        getBeansOfType in interface ListableBeanFactory
        Parameters:
        type - the class or interface to match, or null for all concrete beans
        includeNonSingletons - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
        allowEagerInit - whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
        Returns:
        a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
        Throws:
        BeansException - if a bean could not be created
        See Also:
        FactoryBean.getObjectType(), BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
      • getBeanNamesForAnnotation

        public java.lang.String[] getBeanNamesForAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Description copied from interface: ListableBeanFactory
        Find all names of beans whose Class has the supplied Annotation type, without creating any bean instances yet.
        Specified by:
        getBeanNamesForAnnotation in interface ListableBeanFactory
        Parameters:
        annotationType - the type of annotation to look for
        Returns:
        the names of all matching beans
      • getBeansWithAnnotation

        public java.util.Map<java.lang.String,java.lang.Object> getBeansWithAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Description copied from interface: ListableBeanFactory
        Find all beans whose Class has the supplied Annotation type, returning a Map of bean names with corresponding bean instances.
        Specified by:
        getBeansWithAnnotation in interface ListableBeanFactory
        Parameters:
        annotationType - the type of annotation to look for
        Returns:
        a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
      • findAnnotationOnBean

        @Nullable
        public <A extends java.lang.annotation.Annotation> A findAnnotationOnBean(java.lang.String beanName,
                                                                                            java.lang.Class<A> annotationType)
                                                                                     throws NoSuchBeanDefinitionException
        Find a Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself, as well as checking its raw bean class if not found on the exposed bean reference (e.g. in case of a proxy).
        Specified by:
        findAnnotationOnBean in interface ListableBeanFactory
        Parameters:
        beanName - the name of the bean to look for annotations on
        annotationType - the annotation class to look for
        Returns:
        the annotation of the given type if found, or null otherwise
        Throws:
        NoSuchBeanDefinitionException - if there is no bean with the given name
      • registerResolvableDependency

        public void registerResolvableDependency(java.lang.Class<?> dependencyType,
                                                 @Nullable
                                                 java.lang.Object autowiredValue)
        Description copied from interface: ConfigurableListableBeanFactory
        Register a special dependency type with corresponding autowired value.

        This is intended for factory/context references that are supposed to be autowirable but are not defined as beans in the factory: e.g. a dependency of type ApplicationContext resolved to the ApplicationContext instance that the bean is living in.

        Note: There are no such default types registered in a plain BeanFactory, not even for the BeanFactory interface itself.

        Specified by:
        registerResolvableDependency in interface ConfigurableListableBeanFactory
        Parameters:
        dependencyType - the dependency type to register. This will typically be a base interface such as BeanFactory, with extensions of it resolved as well if declared as an autowiring dependency (e.g. ListableBeanFactory), as long as the given value actually implements the extended interface.
        autowiredValue - the corresponding autowired value. This may also be an implementation of the ObjectFactory interface, which allows for lazy resolution of the actual target value.
      • isAutowireCandidate

        protected boolean isAutowireCandidate(java.lang.String beanName,
                                              DependencyDescriptor descriptor,
                                              AutowireCandidateResolver resolver)
                                       throws NoSuchBeanDefinitionException
        Determine whether the specified bean definition qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type.
        Parameters:
        beanName - the name of the bean definition to check
        descriptor - the descriptor of the dependency to resolve
        resolver - the AutowireCandidateResolver to use for the actual resolution algorithm
        Returns:
        whether the bean should be considered as autowire candidate
        Throws:
        NoSuchBeanDefinitionException
      • isAutowireCandidate

        protected boolean isAutowireCandidate(java.lang.String beanName,
                                              RootBeanDefinition mbd,
                                              DependencyDescriptor descriptor,
                                              AutowireCandidateResolver resolver)
        Determine whether the specified bean definition qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type.
        Parameters:
        beanName - the name of the bean definition to check
        mbd - the merged bean definition to check
        descriptor - the descriptor of the dependency to resolve
        resolver - the AutowireCandidateResolver to use for the actual resolution algorithm
        Returns:
        whether the bean should be considered as autowire candidate
      • freezeConfiguration

        public void freezeConfiguration()
        Description copied from interface: ConfigurableListableBeanFactory
        Freeze all bean definitions, signalling that the registered bean definitions will not be modified or post-processed any further.

        This allows the factory to aggressively cache bean definition metadata.

        Specified by:
        freezeConfiguration in interface ConfigurableListableBeanFactory
      • isBeanEligibleForMetadataCaching

        protected boolean isBeanEligibleForMetadataCaching(java.lang.String beanName)
        Considers all beans as eligible for metadata caching if the factory's configuration has been marked as frozen.
        Overrides:
        isBeanEligibleForMetadataCaching in class AbstractBeanFactory
        Parameters:
        beanName - the name of the bean
        Returns:
        true if the bean's metadata may be cached at this point already
        See Also:
        freezeConfiguration()
      • allowAliasOverriding

        protected boolean allowAliasOverriding()
        Only allows alias overriding if bean definition overriding is allowed.
        Overrides:
        allowAliasOverriding in class SimpleAliasRegistry
      • registerSingleton

        public void registerSingleton(java.lang.String beanName,
                                      java.lang.Object singletonObject)
                               throws java.lang.IllegalStateException
        Description copied from interface: SingletonBeanRegistry
        Register the given existing object as singleton in the bean registry, under the given bean name.

        The given instance is supposed to be fully initialized; the registry will not perform any initialization callbacks (in particular, it won't call InitializingBean's afterPropertiesSet method). The given instance will not receive any destruction callbacks (like DisposableBean's destroy method) either.

        When running within a full BeanFactory: Register a bean definition instead of an existing instance if your bean is supposed to receive initialization and/or destruction callbacks.

        Typically invoked during registry configuration, but can also be used for runtime registration of singletons. As a consequence, a registry implementation should synchronize singleton access; it will have to do this anyway if it supports a BeanFactory's lazy initialization of singletons.

        Specified by:
        registerSingleton in interface SingletonBeanRegistry
        Overrides:
        registerSingleton in class DefaultSingletonBeanRegistry
        Parameters:
        beanName - the name of the bean
        singletonObject - the existing singleton object
        Throws:
        java.lang.IllegalStateException
        See Also:
        InitializingBean.afterPropertiesSet(), DisposableBean.destroy(), BeanDefinitionRegistry.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
      • determinePrimaryCandidate

        @Nullable
        protected java.lang.String determinePrimaryCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
                                                                       java.lang.Class<?> requiredType)
        Determine the primary candidate in the given set of beans.
        Parameters:
        candidates - a Map of candidate names and candidate instances (or candidate classes if not created yet) that match the required type
        requiredType - the target dependency type to match against
        Returns:
        the name of the primary candidate, or null if none found
        See Also:
        isPrimary(String, Object)
      • determineHighestPriorityCandidate

        @Nullable
        protected java.lang.String determineHighestPriorityCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
                                                                               java.lang.Class<?> requiredType)
        Determine the candidate with the highest priority in the given set of beans.

        Based on @javax.annotation.Priority. As defined by the related Ordered interface, the lowest value has the highest priority.

        Parameters:
        candidates - a Map of candidate names and candidate instances (or candidate classes if not created yet) that match the required type
        requiredType - the target dependency type to match against
        Returns:
        the name of the candidate with the highest priority, or null if none found
        See Also:
        getPriority(Object)
      • isPrimary

        protected boolean isPrimary(java.lang.String beanName,
                                    java.lang.Object beanInstance)
        Return whether the bean definition for the given bean name has been marked as a primary bean.
        Parameters:
        beanName - the name of the bean
        beanInstance - the corresponding bean instance (can be null)
        Returns:
        whether the given bean qualifies as primary
      • getPriority

        @Nullable
        protected java.lang.Integer getPriority(java.lang.Object beanInstance)
        Return the priority assigned for the given bean instance by the javax.annotation.Priority annotation.

        The default implementation delegates to the specified dependency comparator, checking its method if it is an extension of Spring's common OrderComparator - typically, an AnnotationAwareOrderComparator. If no such comparator is present, this implementation returns null.

        Parameters:
        beanInstance - the bean instance to check (can be null)
        Returns:
        the priority assigned to that bean or null if none is set
      • matchesBeanName

        protected boolean matchesBeanName(java.lang.String beanName,
                                          @Nullable
                                          java.lang.String candidateName)
        Determine whether the given candidate name matches the bean name or the aliases stored in this bean definition.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • writeReplace

        protected java.lang.Object writeReplace()
                                         throws java.io.ObjectStreamException
        Throws:
        java.io.ObjectStreamException