spring-context解析--容器构造方法
结论
package com.springdemo.demo.interfac;
import com.springdemo.demo.interfac.ee.BB;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration
@ComponentScan("com.springdemo.demo.interfac")
public class Demo {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Demo.class);
Lazy annotation = BB.class.getAnnotation(Lazy.class);
if (annotation == null){
BB bb = annotationConfigApplicationContext.getBean(BB.class);
bb.say();
}
Object environment = annotationConfigApplicationContext.getBean("environment");
System.out.println(environment);
}
}
register方法
- 优先注册启动类(Demo.class)
将这个启动类bean注册到AnnotationConfigApplicationContext
的reader
(BeanDinfinitionRegister的包装类)属性中的,由reader属性来调用GenericApplicationContext中的doRegister,实际上还是在DefaultListableBeanFactory的beanDefinitionMap中- 默认注册4个bean
ConfigurationClassPostProcessor -> 处理@configuration
AutowiredAnnotationBeanPostProcessor -> 处理@autowired、@Lookup、@Value 支持jsr-330@Inject
CommonAnnotationBeanPostProcessor -> 提供@JSR-250的支持
PersistenceAnnotationBeanPostProcessor -> 提供jpa支持
EventListenerMethodProcessor -> 提供@EventListener的支持
DefaultEventListenerFactory -> 提供@EventListener的支持
refresh方法
准备刷新(将早期监听器质空
)->获取beanFactory
->准备刷新容器(注入environment)环境bean、systemProperties(系统属性bean),systemEnvironment(系统环境bean))
->处理所有继承了BeanDefinitionRegistryPostProcessor的bean
-> ``
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// 处理所有的实现了`BeanDefinitionRegistryPostProcessor` 接口的processor
invokeBeanFactoryPostProcessors(beanFactory);
// 处理所有实现了`BeanPostProcessor`接口的processor
registerBeanPostProcessors(beanFactory);
//初始化信息源
initMessageSource();
//初始化事件多种转换器
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
//注册所有实现了ApplicationListener的监听器
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}