Spring Bean 生命周期是很经典的问题,已经有很多详细的文章介绍,从Spring源码注释也可以看到一些端倪。
Spring Version: 5.3.1-SNAPSHOT
Class: org.springframework.beans.factory.BeanFactory
相关注释:
* <p>Bean factory implementations should support the standard bean lifecycle interfaces
* as far as possible. The full set of initialization methods and their standard order is:
* <ol>
* <li>BeanNameAware's {@code setBeanName}
* <li>BeanClassLoaderAware's {@code setBeanClassLoader}
* <li>BeanFactoryAware's {@code setBeanFactory}
* <li>EnvironmentAware's {@code setEnvironment}
* <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
* <li>ResourceLoaderAware's {@code setResourceLoader}
* (only applicable when running in an application context)
* <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
* (only applicable when running in an application context)
* <li>MessageSourceAware's {@code setMessageSource}
* (only applicable when running in an application context)
* <li>ApplicationContextAware's {@code setApplicationContext}
* (only applicable when running in an application context)
* <li>ServletContextAware's {@code setServletContext}
* (only applicable when running in a web application context)
* <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors
* <li>InitializingBean's {@code afterPropertiesSet}
* <li>a custom init-method definition
* <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors
* </ol>
从上面的注释中我们来看看相关类
- org.springframework.beans.factory.BeanNameAware
* Interface to be implemented by beans that want to be aware of their
* bean name in a bean factory. Note that it is not usually recommended
* that an object depends on its bean name, as this represents a potentially
* brittle dependence on external configuration, as well as a possibly
* unnecessary dependence on a Spring API.
- org.springframework.beans.factory.BeanClassLoaderAware
* Callback that allows a bean to be aware of the bean
* {@link ClassLoader class loader}; that is, the class loader used by the
* present bean factory to load bean classes.
*
* <p>This is mainly intended to be implemented by framework classes which
* have to pick up application classes by name despite themselves potentially
* being loaded from a shared class loader.
- org.springframework.beans.factory.BeanFactoryAware
* Interface to be implemented by beans that wish to be aware of their
* owning {@link BeanFactory}.
*
* <p>For example, beans can look up collaborating beans via the factory
* (Dependency Lookup). Note that most beans will choose to receive references
* to collaborating beans via corresponding bean properties or constructor
* arguments (Dependency Injection).
- org.springframework.context.EnvironmentAware
* Interface to be implemented by any bean that wishes to be notified
* of the {@link Environment} that it runs in.
- org.springframework.context.EmbeddedValueResolverAware
* Interface to be implemented by any object that wishes to be notified of a
* {@code StringValueResolver} for the resolution of embedded definition values.
*
* <p>This is an alternative to a full ConfigurableBeanFactory dependency via the
* {@code ApplicationContextAware}/{@code BeanFactoryAware} interfaces.
- org.springframework.context.ResourceLoaderAware
* Interface to be implemented by any object that wishes to be notified of the
* {@link ResourceLoader} (typically the ApplicationContext) that it runs in.
* This is an alternative to a full {@link ApplicationContext} dependency via
* the {@link org.springframework.context.ApplicationContextAware} interface.
*
* <p>Note that {@link org.springframework.core.io.Resource} dependencies can also
* be exposed as bean properties of type {@code Resource}, populated via Strings
* with automatic type conversion by the bean factory. This removes the need for
* implementing any callback interface just for the purpose of accessing a
* specific file resource.
*
* <p>You typically need a {@link ResourceLoader} when your application object has to
* access a variety of file resources whose names are calculated. A good strategy is
* to make the object use a {@link org.springframework.core.io.DefaultResourceLoader}
* but still implement {@code ResourceLoaderAware} to allow for overriding when
* running in an {@code ApplicationContext}. See
* {@link org.springframework.context.support.ReloadableResourceBundleMessageSource}
* for an example.
*
* <p>A passed-in {@code ResourceLoader} can also be checked for the
* {@link org.springframework.core.io.support.ResourcePatternResolver} interface
* and cast accordingly, in order to resolve resource patterns into arrays of
* {@code Resource} objects. This will always work when running in an ApplicationContext
* (since the context interface extends the ResourcePatternResolver interface). Use a
* {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver} as
* default; see also the {@code ResourcePatternUtils.getResourcePatternResolver} method.
*
* <p>As an alternative to a {@code ResourcePatternResolver} dependency, consider
* exposing bean properties of type {@code Resource} array, populated via pattern
* Strings with automatic type conversion by the bean factory at binding time.
- org.springframework.context.ApplicationEventPublisherAware
* Interface to be implemented by any object that wishes to be notified
* of the ApplicationEventPublisher (typically the ApplicationContext)
* that it runs in.
- org.springframework.context.MessageSourceAware
* Interface to be implemented by any object that wishes to be notified
* of the MessageSource (typically the ApplicationContext) that it runs in.
*
* <p>Note that the MessageSource can usually also be passed on as bean
* reference (to arbitrary bean properties or constructor arguments), because
* it is defined as bean with name "messageSource" in the application context.
- org.springframework.context.ApplicationContextAware
* Interface to be implemented by any object that wishes to be notified
* of the {@link ApplicationContext} that it runs in.
*
* <p>Implementing this interface makes sense for example when an object
* requires access to a set of collaborating beans. Note that configuration
* via bean references is preferable to implementing this interface just
* for bean lookup purposes.
*
* <p>This interface can also be implemented if an object needs access to file
* resources, i.e. wants to call {@code getResource}, wants to publish
* an application event, or requires access to the MessageSource. However,
* it is preferable to implement the more specific {@link ResourceLoaderAware},
* {@link ApplicationEventPublisherAware} or {@link MessageSourceAware} interface
* in such a specific scenario.
*
* <p>Note that file resource dependencies can also be exposed as bean properties
* of type {@link org.springframework.core.io.Resource}, populated via Strings
* with automatic type conversion by the bean factory. This removes the need
* for implementing any callback interface just for the purpose of accessing
* a specific file resource.
*
* <p>{@link org.springframework.context.support.ApplicationObjectSupport} is a
* convenience base class for application objects, implementing this interface.
- org.springframework.web.context.ServletContextAware
* Interface to be implemented by any object that wishes to be notified of the
* {@link ServletContext} (typically determined by the {@link WebApplicationContext})
* that it runs in.
- org.springframework.beans.factory.config.BeanPostProcessor
* Factory hook that allows for custom modification of new bean instances —
* for example, checking for marker interfaces or wrapping beans with proxies.
*
* <p>Typically, post-processors that populate beans via marker interfaces
* or the like will implement {@link #postProcessBeforeInitialization},
* while post-processors that wrap beans with proxies will normally
* implement {@link #postProcessAfterInitialization}.
*
* <h3>Registration</h3>
* <p>An {@code ApplicationContext} can autodetect {@code BeanPostProcessor} beans
* in its bean definitions and apply those post-processors to any beans subsequently
* created. A plain {@code BeanFactory} allows for programmatic registration of
* post-processors, applying them to all beans created through the bean factory.
*
* <h3>Ordering</h3>
* <p>{@code BeanPostProcessor} beans that are autodetected in an
* {@code ApplicationContext} will be ordered according to
* {@link org.springframework.core.PriorityOrdered} and
* {@link org.springframework.core.Ordered} semantics. In contrast,
* {@code BeanPostProcessor} beans that are registered programmatically with a
* {@code BeanFactory} will be applied in the order of registration; any ordering
* semantics expressed through implementing the
* {@code PriorityOrdered} or {@code Ordered} interface will be ignored for
* programmatically registered post-processors. Furthermore, the
* {@link org.springframework.core.annotation.Order @Order} annotation is not
* taken into account for {@code BeanPostProcessor} beans.
- org.springframework.beans.factory.InitializingBean
* Interface to be implemented by beans that need to react once all their properties
* have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
* or merely to check that all mandatory properties have been set.
*
* <p>An alternative to implementing {@code InitializingBean} is specifying a custom
* init method, for example in an XML bean definition. For a list of all bean
* lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
- org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor
* Subinterface of {@link BeanPostProcessor} that adds a before-destruction callback.
*
* <p>The typical usage will be to invoke custom destruction callbacks on
* specific bean types, matching corresponding initialization callbacks.