Spring的核心步骤:初始化Bean
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
//ConversionService.class用于类型转换的服务接口
//详见 :Spring中的数据转换:Converter、ConversionService、TypeConverter、PropertyEditor
//举个例子,使用Environment的<T> T getProperty(String key, Class<T> targetType)这里的类型转换,就是要通过ConversionService来完成的。
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded(内部) value resolver if no bean post-processor
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
// 设置一个内置的值处理器(若没有的话),该处理器作用有点像一个PropertyPlaceholderConfigurer bean。(PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去)
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
// 注意此处已经调用了getBean方法,初始化LoadTimeWeaverAware Bean
// getBean()方法的详细,下面会详细分解
// LoadTimeWeaverAware是类加载时织入的意思
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
// 停止使用临时的类加载器
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
// 缓存(冻结)所有的bean definition数据,不期望以后会改变
beanFactory.freezeConfiguration();
// 这个就是最重要的方法:会把留下来的Bean们 不是lazy懒加载的bean都实例化掉 // bean真正实例化的时刻到了
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
}
//Bean要开始创建了
public void preInstantiateSingletons() throws BeansException {
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
//不是抽象的、单例的、并且不是懒加载的
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
//判断是否是FactoryBean、如果不是的话直接获取Bean
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
//如果是Factorybean的话,实现了SmartFactoryBean 接口,并且运行提前将真实的Bean加入到容器 ,则将FactoryBean对应的真实Bean也加入到容器当中
if (isEagerInit) {
//具体获取Bean的过程在就不在这个地方将了,
//单独拿出一个专栏来讲这是jSpring核心
//详见: Spring 获取和创建Bean的过程分析
getBean(beanName);
}
}
}else {
//具体获取Bean的过程在就不在这个地方将了,
//单独拿出一个专栏来讲这是jSpring核心
//详见: Spring 获取和创建Bean的过程分析
getBean(beanName);
}
}
}
// Trigger post-initialization callback for all applicable beans...
//对于实现SmartInitializingSingleton的接口,在Bean初始化完成之后,进行调用处理一些其他事情。很重要,
//在后边分析Spring Cloud系列组件的时候分析这地方的应用
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}