1、 Bean 的生命週期
* <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>
*
* <p>On shutdown of a bean factory, the following lifecycle methods apply:
* <ol>
* <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
* <li>DisposableBean's {@code destroy}
* <li>a custom destroy-method definition
* </ol>
這個是Bean 運行的生命週期。
? 這個不太明白,後續補上。
/**
* Used to dereference a {@link FactoryBean} instance and distinguish it from
* beans <i>created</i> by the FactoryBean. For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*/
String FACTORY_BEAN_PREFIX = "&";
?通過name 進行實例化對象。
Object getBean(String name) throws BeansException;
? 這個方法添加指定實例化bean的類型。
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
?這個通過指定實例化的構造函數參數。
Object getBean(String name, Object... args) throws BeansException;
?通過類型進行匹配進行實例化,這個無參構造函數進行實例化。
<T> T getBean(Class<T> requiredType) throws BeansException;
?通過匹配的類型,構造函數進行實例化。
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
?構造一個類型提供器。
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
這個看下其實現,發現內部也是進行實例化,進行了一層的封裝,暫不清楚爲啥會在進行封裝一個這個類。
@Override
public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {
return new ObjectProvider<T>() {
@Override
public T getObject() throws BeansException {
return getBean(requiredType);
}
@Override
public T getObject(Object... args) throws BeansException {
return getBean(requiredType, args);
}
@Override
@Nullable
public T getIfAvailable() throws BeansException {
try {
return getBean(requiredType);
}
catch (NoUniqueBeanDefinitionException ex) {
throw ex;
}
catch (NoSuchBeanDefinitionException ex) {
return null;
}
}
@Override
@Nullable
public T getIfUnique() throws BeansException {
try {
return getBean(requiredType);
}
catch (NoSuchBeanDefinitionException ex) {
return null;
}
}
};
}
看ObjectProvider 類
/**
* A variant of {@link ObjectFactory} designed specifically for injection points,
* allowing for programmatic optionality and lenient not-unique handling.
*/
在這個類中提供了這個方法
@Override
default Iterator<T> iterator() {
return stream().iterator();
}
說明通過這類可以遍歷處理多個實例。而BeanFactory 的其他方法都是返回一個實例對象。這裏加載多個相同類的實例provider。
從註釋可以發現這個是5.1 增加的方法。