public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
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:
BeanNameAware :可以获取容器中bean的名称
BeanFactoryAware:获取当前bean factory这也可以调用容器的服务
ApplicationContextAware: 当前的applicationContext, 这也可以调用容器的服务
MessageSourceAware:获得message source,这也可以获得文本信息
applicationEventPulisherAware:应用事件发布器,可以发布事件,
ResourceLoaderAware: 获得资源加载器,可以获得外部资源文件的内容;
* <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>
* <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
* <li>DisposableBean‘s {@code destroy}
* <li>a custom destroy-method definition
* </ol>
一个依赖于BeanFactory,生产个性化Bean的接口,因此它也是synchronization同步的
getObjectType() , getObject() 这两个主要方法,在启动的时候进行暴露,甚至在post-processor启动之前
@link #getObjectType()} {@link #getObject()} invocations may arrive early in
the bootstrap process, even ahead of any post-processor setup.
A bean that implements this interface cannot be used as a normal bean.
A FactoryBean is defined in a bean style
ConfigurableEnvironment
PropertySourcesPlaceholderConfigurer
@Profile 不同的环境下使用不同的@Configuration配置
组合类 StandardEnvironment
extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver
一个接口代表一个能力的抽象,子接口通过继承父接口,整合了父接口的功能
<p>An ApplicationContext provides:
* <li>Bean factory methods for accessing application components.
* Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}.
* <li>The ability to load file resources in a generic fashion.
* Inherited from the {@link org.springframework.core.io.ResourceLoader} interface.
* <li>The ability to publish events to registered listeners.
* Inherited from the {@link ApplicationEventPublisher} interface.
* <li>The ability to resolve messages, supporting internationalization.
* Inherited from the {@link MessageSource} interface.
整合的能力:
一个父上下文中可以使用整个web应用程序,而每个servlet有自己的子上下文无关其他servlet
ListableBeanFactory extends BeanFactory
HierarchicalBeanFactory extends BeanFactory
暴露 Environment getEnvironment();
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable
ConfigurableApplicationContext 继承了ApplicationContext 能力,它重新覆盖了EnvironmentCapable的getEnvironment方法,并且返回的是 ConfigurableEnvironment 事实上 Environment是只读的。ConfigurableEnvironment 是可以被配置的,进行个性定制。
@FunctionalInterface
public interface ApplicationEventPublisher {
拥有自动装配能力,并把这种能力暴露给外部应用的BeanFactory类,需要实现此接口
其他框架,利用该接口可以连接和填充Bean实例,且不受Spring控制
ApplicationContext通过方法 getAutowireCapableBeanFactory() 可以获取AutowireCapableBeanFactory
ListableBeanFactory 扩展BeanFactory ,把所有的Bean实例列举出来,而不是一个个用name去找
HierarchicalBeanFactory 分层工厂 (2个方法)
//返回工厂的父工厂
BeanFactory getParentBeanFactory();
//这个工厂中是否包含这个Bean
boolean containsLocalBean(String name);
ConfigurableListableBeanFactory 集成上图的所有接口的功能,加上自身的可配置性
短剑 + 短剑 = 小黄刀 , 黄刀 + 暴风大剑 = 无尽
Strategy interface for resolving a location pattern
策略接口,解析本地路径的资源
策略接口 资源加载器
classpath*: 搜索所有jar包
classpath: 当前路径搜索
ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
原文:https://www.cnblogs.com/JMrLi/p/13039225.html