org.springframework.context.support

Class AbstractApplicationContext

    • Field Detail

      • MESSAGE_SOURCE_BEAN_NAME

        public static final java.lang.String MESSAGE_SOURCE_BEAN_NAME
        Name of the MessageSource bean in the factory. If none is supplied, message resolution is delegated to the parent.
        See Also:
        MessageSource, Constant Field Values
      • logger

        protected final Log logger
        Logger used by this class. Available to subclasses.
    • Constructor Detail

      • AbstractApplicationContext

        public AbstractApplicationContext()
        Create a new AbstractApplicationContext with no parent.
      • AbstractApplicationContext

        public AbstractApplicationContext(@Nullable
                                          ApplicationContext parent)
        Create a new AbstractApplicationContext with the given parent context.
        Parameters:
        parent - the parent context
    • Method Detail

      • setId

        public void setId(java.lang.String id)
        Set the unique id of this application context.

        Default is the object id of the context instance, or the name of the context bean if the context is itself defined as a bean.

        Specified by:
        setId in interface ConfigurableApplicationContext
        Parameters:
        id - the unique id of the context
      • getId

        public java.lang.String getId()
        Description copied from interface: ApplicationContext
        Return the unique id of this application context.
        Specified by:
        getId in interface ApplicationContext
        Returns:
        the unique id of the context, or null if none
      • getApplicationName

        public java.lang.String getApplicationName()
        Description copied from interface: ApplicationContext
        Return a name for the deployed application that this context belongs to.
        Specified by:
        getApplicationName in interface ApplicationContext
        Returns:
        a name for the deployed application, or the empty String by default
      • setDisplayName

        public void setDisplayName(java.lang.String displayName)
        Set a friendly name for this context. Typically done during initialization of concrete context implementations.

        Default is the object id of the context instance.

      • getDisplayName

        public java.lang.String getDisplayName()
        Return a friendly name for this context.
        Specified by:
        getDisplayName in interface ApplicationContext
        Returns:
        a display name for this context (never null)
      • getParent

        @Nullable
        public ApplicationContext getParent()
        Return the parent context, or null if there is no parent (that is, this context is the root of the context hierarchy).
        Specified by:
        getParent in interface ApplicationContext
        Returns:
        the parent context, or null if there is no parent
      • getAutowireCapableBeanFactory

        public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
                                                                 throws java.lang.IllegalStateException
        Return this context's internal bean factory as AutowireCapableBeanFactory, if already available.
        Specified by:
        getAutowireCapableBeanFactory in interface ApplicationContext
        Returns:
        the AutowireCapableBeanFactory for this context
        Throws:
        java.lang.IllegalStateException - if the context does not support the AutowireCapableBeanFactory interface, or does not hold an autowire-capable bean factory yet (e.g. if refresh() has never been called), or if the context has been closed already
        See Also:
        getBeanFactory()
      • getStartupDate

        public long getStartupDate()
        Return the timestamp (ms) when this context was first loaded.
        Specified by:
        getStartupDate in interface ApplicationContext
        Returns:
        the timestamp (ms) when this context was first loaded
      • publishEvent

        public void publishEvent(ApplicationEvent event)
        Publish the given event to all listeners.

        Note: Listeners get initialized after the MessageSource, to be able to access it within listener implementations. Thus, MessageSource implementations cannot publish events.

        Specified by:
        publishEvent in interface ApplicationEventPublisher
        Parameters:
        event - the event to publish (may be application-specific or a standard framework event)
        See Also:
        RequestHandledEvent
      • publishEvent

        public void publishEvent(java.lang.Object event)
        Publish the given event to all listeners.

        Note: Listeners get initialized after the MessageSource, to be able to access it within listener implementations. Thus, MessageSource implementations cannot publish events.

        Specified by:
        publishEvent in interface ApplicationEventPublisher
        Parameters:
        event - the event to publish (may be an ApplicationEvent or a payload object to be turned into a PayloadApplicationEvent)
        See Also:
        PayloadApplicationEvent
      • publishEvent

        protected void publishEvent(java.lang.Object event,
                                    @Nullable
                                    ResolvableType eventType)
        Publish the given event to all listeners.
        Parameters:
        event - the event to publish (may be an ApplicationEvent or a payload object to be turned into a PayloadApplicationEvent)
        eventType - the resolved event type, if known
        Since:
        4.2
      • getResourcePatternResolver

        protected ResourcePatternResolver getResourcePatternResolver()
        Return the ResourcePatternResolver to use for resolving location patterns into Resource instances. Default is a PathMatchingResourcePatternResolver, supporting Ant-style location patterns.

        Can be overridden in subclasses, for extended resolution strategies, for example in a web environment.

        Do not call this when needing to resolve a location pattern. Call the context's getResources method instead, which will delegate to the ResourcePatternResolver.

        Returns:
        the ResourcePatternResolver for this context
        See Also:
        getResources(java.lang.String), PathMatchingResourcePatternResolver
      • getBeanFactoryPostProcessors

        public java.util.List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors()
        Return the list of BeanFactoryPostProcessors that will get applied to the internal BeanFactory.
      • getApplicationListeners

        public java.util.Collection<ApplicationListener<?>> getApplicationListeners()
        Return the list of statically specified ApplicationListeners.
      • refresh

        public void refresh()
                     throws BeansException,
                            java.lang.IllegalStateException
        Description copied from interface: ConfigurableApplicationContext
        Load or refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema.

        As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of that method, either all or no singletons at all should be instantiated.

        Specified by:
        refresh in interface ConfigurableApplicationContext
        Throws:
        BeansException - if the bean factory could not be initialized
        java.lang.IllegalStateException - if already initialized and multiple refresh attempts are not supported
      • prepareRefresh

        protected void prepareRefresh()
        Prepare this context for refreshing, setting its startup date and active flag as well as performing any initialization of property sources.
      • prepareBeanFactory

        protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory)
        Configure the factory's standard context characteristics, such as the context's ClassLoader and post-processors.
        Parameters:
        beanFactory - the BeanFactory to configure
      • postProcessBeanFactory

        protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
        Modify the application context's internal bean factory after its standard initialization. All bean definitions will have been loaded, but no beans will have been instantiated yet. This allows for registering special BeanPostProcessors etc in certain ApplicationContext implementations.
        Parameters:
        beanFactory - the bean factory used by the application context
      • invokeBeanFactoryPostProcessors

        protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)
        Instantiate and invoke all registered BeanFactoryPostProcessor beans, respecting explicit order if given.

        Must be called before singleton instantiation.

      • registerBeanPostProcessors

        protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory)
        Instantiate and invoke all registered BeanPostProcessor beans, respecting explicit order if given.

        Must be called before any instantiation of application beans.

      • initMessageSource

        protected void initMessageSource()
        Initialize the MessageSource. Use parent's if none defined in this context.
      • initApplicationEventMulticaster

        protected void initApplicationEventMulticaster()
        Initialize the ApplicationEventMulticaster. Uses SimpleApplicationEventMulticaster if none defined in the context.
        See Also:
        SimpleApplicationEventMulticaster
      • initLifecycleProcessor

        protected void initLifecycleProcessor()
        Initialize the LifecycleProcessor. Uses DefaultLifecycleProcessor if none defined in the context.
        See Also:
        DefaultLifecycleProcessor
      • onRefresh

        protected void onRefresh()
                          throws BeansException
        Template method which can be overridden to add context-specific refresh work. Called on initialization of special beans, before instantiation of singletons.

        This implementation is empty.

        Throws:
        BeansException - in case of errors
        See Also:
        refresh()
      • registerListeners

        protected void registerListeners()
        Add beans that implement ApplicationListener as listeners. Doesn't affect other listeners, which can be added without being beans.
      • finishBeanFactoryInitialization

        protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)
        Finish the initialization of this context's bean factory, initializing all remaining singleton beans.
      • finishRefresh

        protected void finishRefresh()
        Finish the refresh of this context, invoking the LifecycleProcessor's onRefresh() method and publishing the ContextRefreshedEvent.
      • cancelRefresh

        protected void cancelRefresh(BeansException ex)
        Cancel this context's refresh attempt, resetting the active flag after an exception got thrown.
        Parameters:
        ex - the exception that led to the cancellation
      • registerShutdownHook

        public void registerShutdownHook()
        Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

        Delegates to doClose() for the actual closing procedure.

        Specified by:
        registerShutdownHook in interface ConfigurableApplicationContext
        See Also:
        Runtime.addShutdownHook(java.lang.Thread), close(), doClose()
      • destroy

        @Deprecated
        public void destroy()
        Deprecated. as of Spring Framework 5.0, in favor of close()
        Callback for destruction of this instance, originally attached to a DisposableBean implementation (not anymore in 5.0).

        The close() method is the native way to shut down an ApplicationContext, which this method simply delegates to.

      • close

        public void close()
        Close this application context, destroying all beans in its bean factory.

        Delegates to doClose() for the actual closing procedure. Also removes a JVM shutdown hook, if registered, as it's not needed anymore.

        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface ConfigurableApplicationContext
        See Also:
        doClose(), registerShutdownHook()
      • doClose

        protected void doClose()
        Actually performs context closing: publishes a ContextClosedEvent and destroys the singletons in the bean factory of this application context.

        Called by both close() and a JVM shutdown hook, if any.

        See Also:
        ContextClosedEvent, destroyBeans(), close(), registerShutdownHook()
      • destroyBeans

        protected void destroyBeans()
        Template method for destroying all beans that this context manages. The default implementation destroy all cached singletons in this context, invoking DisposableBean.destroy() and/or the specified "destroy-method".

        Can be overridden to add context-specific bean destruction steps right before or right after standard singleton destruction, while the context's BeanFactory is still active.

        See Also:
        getBeanFactory(), ConfigurableBeanFactory.destroySingletons()
      • onClose

        protected void onClose()
        Template method which can be overridden to add context-specific shutdown work. The default implementation is empty.

        Called at the end of doClose()'s shutdown procedure, after this context's BeanFactory has been closed. If custom shutdown logic needs to execute while the BeanFactory is still active, override the destroyBeans() method instead.

      • assertBeanFactoryActive

        protected void assertBeanFactoryActive()
        Assert that this context's BeanFactory is currently active, throwing an IllegalStateException if it isn't.

        Invoked by all BeanFactory delegation methods that depend on an active context, i.e. in particular all bean accessor methods.

        The default implementation checks the 'active' status of this context overall. May be overridden for more specific checks, or for a no-op if getBeanFactory() itself throws an exception in such a case.

      • getBean

        public java.lang.Object getBean(java.lang.String name)
                                 throws BeansException
        Description copied from interface: BeanFactory
        Return an instance, which may be shared or independent, of the specified bean.

        This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans.

        Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

        Specified by:
        getBean in interface BeanFactory
        Parameters:
        name - the name of the bean to retrieve
        Returns:
        an instance of the bean
        Throws:
        NoSuchBeanDefinitionException - if there is no bean definition with the specified name
        BeansException - if the bean could not be obtained
      • getBean

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

        Behaves the same as BeanFactory.getBean(String), but provides a measure of type safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with BeanFactory.getBean(String).

        Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.

        Specified by:
        getBean in interface BeanFactory
        Parameters:
        name - the name of the bean to retrieve
        requiredType - type the bean must match; can be an interface or superclass
        Returns:
        an instance of the bean
        Throws:
        NoSuchBeanDefinitionException - if there is no such bean definition
        BeanNotOfRequiredTypeException - if the bean is not of the required type
        BeansException - if the bean could not be created
      • getBean

        public java.lang.Object getBean(java.lang.String name,
                                        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.

        Specified by:
        getBean in interface BeanFactory
        Parameters:
        name - the name of the bean to retrieve
        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
      • getBean

        public <T> T getBean(java.lang.Class<T> requiredType,
                             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(java.lang.Class<T> 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 an interface or superclass
        Returns:
        a corresponding provider handle
        See Also:
        BeanFactory.getBeanProvider(ResolvableType)
      • 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()
      • containsBean

        public boolean containsBean(java.lang.String name)
        Description copied from interface: BeanFactory
        Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

        If the given name is an alias, it will be translated back to the corresponding canonical bean name.

        If this factory is hierarchical, will ask any parent factory if the bean cannot be found in this factory instance.

        If a bean definition or singleton instance matching the given name is found, this method will return true whether the named bean definition is concrete or abstract, lazy or eager, in scope or not. Therefore, note that a true return value from this method does not necessarily indicate that BeanFactory.getBean(java.lang.String) will be able to obtain an instance for the same name.

        Specified by:
        containsBean in interface BeanFactory
        Parameters:
        name - the name of the bean to query
        Returns:
        whether a bean with the given name is present
      • getAliases

        public java.lang.String[] getAliases(java.lang.String name)
        Description copied from interface: BeanFactory
        Return the aliases for the given bean name, if any. All of those aliases point to the same bean when used in a BeanFactory.getBean(java.lang.String) call.

        If the given name is an alias, the corresponding original bean name and other aliases (if any) will be returned, with the original bean name being the first element in the array.

        Will ask the parent factory if the bean cannot be found in this factory instance.

        Specified by:
        getAliases in interface BeanFactory
        Parameters:
        name - the bean name to check for aliases
        Returns:
        the aliases, or an empty array if none
        See Also:
        BeanFactory.getBean(java.lang.String)
      • containsBeanDefinition

        public boolean containsBeanDefinition(java.lang.String beanName)
        Description copied from interface: ListableBeanFactory
        Check if this bean factory contains a bean definition with the given name.

        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:
        containsBeanDefinition in interface ListableBeanFactory
        Parameters:
        beanName - the name of the bean to look for
        Returns:
        if this bean factory contains a bean definition with the given name
        See Also:
        BeanFactory.containsBean(java.lang.String)
      • 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
        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
        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)
                                                                                throws BeansException
        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
        Throws:
        BeansException - if a bean could not be created
      • findAnnotationOnBean

        @Nullable
        public <A extends java.lang.annotation.Annotation> A findAnnotationOnBean(java.lang.String beanName,
                                                                                            java.lang.Class<A> annotationType)
                                                                                     throws NoSuchBeanDefinitionException
        Description copied from interface: ListableBeanFactory
        Find an Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself.
        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
      • containsLocalBean

        public boolean containsLocalBean(java.lang.String name)
        Description copied from interface: HierarchicalBeanFactory
        Return whether the local bean factory contains a bean of the given name, ignoring beans defined in ancestor contexts.

        This is an alternative to containsBean, ignoring a bean of the given name from an ancestor bean factory.

        Specified by:
        containsLocalBean in interface HierarchicalBeanFactory
        Parameters:
        name - the name of the bean to query
        Returns:
        whether a bean with the given name is defined in the local factory
        See Also:
        BeanFactory.containsBean(java.lang.String)
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           @Nullable
                                           java.lang.String defaultMessage,
                                           java.util.Locale locale)
        Description copied from interface: MessageSource
        Try to resolve the message. Return default message if no message was found.
        Specified by:
        getMessage in interface MessageSource
        Parameters:
        code - the code to lookup up, such as 'calculator.noRateSet'. Users of this class are encouraged to base message names on the relevant fully qualified class name, thus avoiding conflict and ensuring maximum clarity.
        args - an array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
        defaultMessage - a default message to return if the lookup fails
        locale - the locale in which to do the lookup
        Returns:
        the resolved message if the lookup was successful; otherwise the default message passed as a parameter
        See Also:
        MessageFormat
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           java.util.Locale locale)
                                    throws NoSuchMessageException
        Description copied from interface: MessageSource
        Try to resolve the message. Treat as an error if the message can't be found.
        Specified by:
        getMessage in interface MessageSource
        Parameters:
        code - the code to lookup up, such as 'calculator.noRateSet'
        args - an array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
        locale - the locale in which to do the lookup
        Returns:
        the resolved message
        Throws:
        NoSuchMessageException - if the message wasn't found
        See Also:
        MessageFormat
      • getMessage

        public java.lang.String getMessage(MessageSourceResolvable resolvable,
                                           java.util.Locale locale)
                                    throws NoSuchMessageException
        Description copied from interface: MessageSource
        Try to resolve the message using all the attributes contained within the MessageSourceResolvable argument that was passed in.

        NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage property of the resolvable is null or not.

        Specified by:
        getMessage in interface MessageSource
        Parameters:
        resolvable - the value object storing attributes required to resolve a message
        locale - the locale in which to do the lookup
        Returns:
        the resolved message
        Throws:
        NoSuchMessageException - if the message wasn't found
        See Also:
        MessageFormat
      • getInternalParentMessageSource

        @Nullable
        protected MessageSource getInternalParentMessageSource()
        Return the internal message source of the parent context if it is an AbstractApplicationContext too; else, return the parent context itself.
      • getResources

        public Resource[] getResources(java.lang.String locationPattern)
                                throws java.io.IOException
        Description copied from interface: ResourcePatternResolver
        Resolve the given location pattern into Resource objects.

        Overlapping resource entries that point to the same physical resource should be avoided, as far as possible. The result should have set semantics.

        Specified by:
        getResources in interface ResourcePatternResolver
        Parameters:
        locationPattern - the location pattern to resolve
        Returns:
        the corresponding Resource objects
        Throws:
        java.io.IOException - in case of I/O errors
      • start

        public void start()
        Description copied from interface: Lifecycle
        Start this component.

        Should not throw an exception if the component is already running.

        In the case of a container, this will propagate the start signal to all components that apply.

        Specified by:
        start in interface Lifecycle
        See Also:
        SmartLifecycle.isAutoStartup()
      • stop

        public void stop()
        Description copied from interface: Lifecycle
        Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing SmartLifecycle and its stop(Runnable) variant when asynchronous stop behavior is necessary.

        Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.

        Should not throw an exception if the component is not running (not started yet).

        In the case of a container, this will propagate the stop signal to all components that apply.

        Specified by:
        stop in interface Lifecycle
        See Also:
        SmartLifecycle.stop(Runnable), DisposableBean.destroy()
      • isRunning

        public boolean isRunning()
        Description copied from interface: Lifecycle
        Check whether this component is currently running.

        In the case of a container, this will return true only if all components that apply are currently running.

        Specified by:
        isRunning in interface Lifecycle
        Returns:
        whether the component is currently running
      • refreshBeanFactory

        protected abstract void refreshBeanFactory()
                                            throws BeansException,
                                                   java.lang.IllegalStateException
        Subclasses must implement this method to perform the actual configuration load. The method is invoked by refresh() before any other initialization work.

        A subclass will either create a new bean factory and hold a reference to it, or return a single BeanFactory instance that it holds. In the latter case, it will usually throw an IllegalStateException if refreshing the context more than once.

        Throws:
        BeansException - if initialization of the bean factory failed
        java.lang.IllegalStateException - if already initialized and multiple refresh attempts are not supported
      • closeBeanFactory

        protected abstract void closeBeanFactory()
        Subclasses must implement this method to release their internal bean factory. This method gets invoked by close() after all other shutdown work.

        Should never throw an exception but rather log shutdown failures.

      • getBeanFactory

        public abstract ConfigurableListableBeanFactory getBeanFactory()
                                                                throws java.lang.IllegalStateException
        Subclasses must return their internal bean factory here. They should implement the lookup efficiently, so that it can be called repeatedly without a performance penalty.

        Note: Subclasses should check whether the context is still active before returning the internal bean factory. The internal factory should generally be considered unavailable once the context has been closed.

        Specified by:
        getBeanFactory in interface ConfigurableApplicationContext
        Returns:
        this application context's internal bean factory (never null)
        Throws:
        java.lang.IllegalStateException - if the context does not hold an internal bean factory yet (usually if refresh() has never been called) or if the context has been closed already
        See Also:
        refreshBeanFactory(), closeBeanFactory()
      • toString

        public java.lang.String toString()
        Return information about this context.
        Overrides:
        toString in class java.lang.Object