代码层面:
// SpringApplication line 311 context = createApplicationContext();
//细节public class SpringApplication{ //web 容器创建context public static final String DEFAULT_WEB_CONTEXT_CLASS = "org.springframework." + "boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext”; //非web容器创建context public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context." + "annotation.AnnotationConfigApplicationContext”;
//我们后续以webEnvironment为基础探讨: protected ConfigurableApplicationContext createApplicationContext() { //.... contextClass = Class.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS); //..... return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);==>clazz.newInstance(); //.... }}
复制代码
Context 和 BeanDefinitionRegistry 和 DefaultListableBeanFactory 之间的关系如下图:
分析:new context() 过程
相关父类 construct 如下:
// AnnotationConfigEmbeddedWebApplicationContext.construct()public AnnotationConfigEmbeddedWebApplicationContext() { this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this);}
// EmbeddedWebApplicationContext.construct()默认无参构造函数
//GenericWebApplicationContext.construct() public GenericWebApplicationContext() { super(); } public GenericWebApplicationContext(ServletContext servletContext) { this.servletContext = servletContext; } public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory) { super(beanFactory); }
public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory, ServletContext servletContext) { super(beanFactory); this.servletContext = servletContext; }
//GenericApplicationContext.construct() public GenericApplicationContext() { this.beanFactory = new DefaultListableBeanFactory(); }
public GenericApplicationContext(DefaultListableBeanFactory beanFactory) { Assert.notNull(beanFactory, "BeanFactory must not be null"); this.beanFactory = beanFactory; } public GenericApplicationContext(ApplicationContext parent) { this(); setParent(parent); }
public GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent) { this(beanFactory); setParent(parent); }
// AbstractApplicationContext.construct() public AbstractApplicationContext() { this.resourcePatternResolver = getResourcePatternResolver(); } public AbstractApplicationContext(ApplicationContext parent) { this(); setParent(parent); }
复制代码
getResourcePatternResolver()执行流程
// GenericWebApplicationContext.getResourcePatternResolver()
this=AnnotationConfigEmbeddedWebApplicationContext()
new DefaultListableBeanFactory()执行流程
这里没有太多的内容,主要就是创建对象,不细分
new AnnotatedBeanDefinitionReader(this);
(1)创建环境: environment = new StandardServletEnvironment();
(2) conditionEvaluator 为了后续处理 conditional 注解
(3) AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
(4)注册 Processors 到 2 的 beanFactory
new ClassPathBeanDefinitionScanner(this);
(1) 使用默认的过滤策略
(2) 环境有获取,没有创建。和前文的环境是同一个
(3) 设置 ClassPathBeanDefinitionScanner 属性
其他补充:
在 prepareContext 阶段将 SpringBootAplication 启动类加载进来
AnnotatedBeanDefinitionReader.register(class t);
评论