SpringApplication

SpringApplication

定义:Spring应用引导类,提供便利的自定义行为方法
场景:嵌入式Web应用和非Web应用

准备阶段

  • 配置:Spring Bean来源
    • Java配置Class:Spring注解驱动中Java配置类,大多是情况下是Spring 模式注解锁标注的类,如被@configuration标注的类
    • XML上下文配置文件:用于Spring 传统配置驱动中的xml文件
BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
        Assert.notNull(registry, "Registry must not be null");
        Assert.notEmpty(sources, "Sources must not be empty");
        this.sources = sources;
        // 使用AnnotatedBeanDefinitionReader进行配置
        this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
        // 使用XmlBeanDefinitionReader进行配置
        this.xmlReader = XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null;
        this.groovyReader = this.isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null;
        this.scanner = new ClassPathBeanDefinitionScanner(registry);
        this.scanner.addExcludeFilter(new BeanDefinitionLoader.ClassExcludeFilter(sources));
    }
  • 推断:Web应用类型 和 主引导类(Main Class)
    • Web Reactive:webApplicaitonType.REACTIVE
    • Web Servlet:webApplicaitonType.SERVLET
static WebApplicationType deduceFromClasspath() {
        // WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
        // WEBMVC_INDICATOR_CLASS = "org.springframework.web.servlet.DispatcherServlet";
        // JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";
        // 如果没有上面的方式,默认使用Servlet_indicator_classes,如果共存的情况下优先使用Servlet_indicator_classes
		if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
			return WebApplicationType.REACTIVE;
		}
		for (String className : SERVLET_INDICATOR_CLASSES) {
			if (!ClassUtils.isPresent(className, null)) {
				return WebApplicationType.NONE;
			}
		}
		return WebApplicationType.SERVLET;
	}

 可以在引导类中指定引导的具体类型

/**
 * {@link SpringApplication} 引导类
 */

public class SpringApplicationBootstrap {
    public static void main(String[] args) {
        // 交给其运行的类只要上面有@SpringBootApplicat ion注解就行,不一定是主类名
        // SpringApplication.run(ApplicationConfiguration.class, args);
        HashSet<String> set = new HashSet<>();
        set.add(ApplicationConfiguration.class.getName());
        SpringApplication springApplication = new SpringApplication();
        springApplication.setSources(set);
        // 默认为Servlet类型,此处对它进行强制关闭,变成普通类型--主线程会中断[非web容器], web容器主线程会阻塞等待请求
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        ConfigurableApplicationContext context = springApplication.run(args);
        // 反射EnhancerBySpringCGLIB
        // com.SpringBoot.study.SpringApplicationBootstrap$ApplicationConfiguration$$EnhancerBySpringCGLIB$$ea8d68dc@400d912a
        System.out.println(context.getBean(ApplicationConfiguration.class));
    }
    
    @SpringBootApplication
    public static class ApplicationConfiguration{
    }
}

  • 加载:应用上下文初始化器 和 应用事件监听器
    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    		this.resourceLoader = resourceLoader;
    		Assert.notNull(primarySources, "PrimarySources must not be null");
    		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    		this.webApplicationType = WebApplicationType.deduceFromClasspath();
    		this.bootstrappers = new ArrayList<>(getSpringFactoriesInstances(Bootstrapper.class));
            // 上下文初始化器
    		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
            // 监听器
    		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    		this.mainApplicationClass = deduceMainApplicationClass();
    	}
    • 上下文初始化器技术
      • 实现类:org.springframework.core.io.support.SpringFactoriesLoader
      • 配置资源:META-INF/spring.factories
      • 排序:AnnotationAwareOrder

《慕课网--深入Spring Boot2.0》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值