SpringBoot之旅-启动原理及自定义starter
public static ConfigurableApplicationContext run(Class<?>[] primarySources,String[] args) {return new SpringApplication(primarySources).run(args);}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {this.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");//保存主配置类this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));//确定web应用类型this.webApplicationType = WebApplicationType.deduceFromClasspath();//从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//从类路径下找到ETA-INF/spring.factories配置的所有ApplicationListenersetListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//从多个配置类中找到有main方法的主配置类this.main
public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();configureHeadlessProperty();//从类路径下META-INF/spring.factories获取SpringApplicationRunListenersSpringApplicationRunListeners listeners = getRunListeners(args);//回调所有的获取SpringApplicationRunListener.starting()方法listeners.starting();try {//封装命令行参数ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//准备环境ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);//创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成configureIgnoreBeanInfo(environment);//打印Banner图Banner printedBanner = printBanner(environment);//创建ApplicationContext,决定创建web的ioc还是普通的ioccontext = createApplicationContext();//异常分析报告exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);//准备上下文环境,将environment保存到ioc中//applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法//listeners.contextPrepared(context)//prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded()prepareContext(context, environment, listeners, applicationArguments,printedBanner);//刷新容器,ioc容器初始化(如果是web应用还会创建嵌入式的Tomcat)//扫描,创建,加载所有组件的地方,(配置类,组件,自动配置)refreshContext(context);afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}//所有的SpringApplicationRunListener回调started方法listeners.started(context);//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调,//ApplicationRunner先回调,CommandLineRunner再回调callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, listeners);throw new IllegalStateException(ex);}try {//所有的SpringApplicationRunListener回调running方法listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, null);throw new IllegalStateException(ex);}//整个SpringBoot应用启动完成以后返回启动的ioc容器return context;}
<dependencies><!--引入自动配置模块--><dependency><groupId>com.yuanqinnan-starter</groupId><artifactId>yuanqinnan-springboot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.yuanqinnan-starter</groupId><artifactId>yuanqinnan-springboot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><properties><java.version>1.8</java.version></properties><dependencies><!--引入spring-boot-starter;所有starter的基本配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies></project>
@ConfigurationProperties(prefix = "yuanqinnan.hello")public class HelloProperties {//前缀private String prefix;//后缀private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}}
public class HelloService {HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}public String sayHello(String name) {return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();}}
@Configuration@ConditionalOnWebApplication //web应用才生效@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration {@AutowiredHelloProperties helloProperties;@Beanpublic HelloService helloService(){HelloService service = new HelloService();service.setHelloProperties(helloProperties);return service;}}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.yuanqinnan.starter.HelloServiceAutoConfiguration
<!--引入自定义starter--><dependency><groupId>com.yuanqinnan.starter</groupId><artifactId>yuanqinnan-springboot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>
yuanqinnan.hello.prefix=早安yuanqinnan.hello.suffix=晚安
@AutowiredHelloService helloService;@Testpublic void contextLoads() {System.out.println(helloService.sayHello("世界"));}

剩下的就不会给大家一展出来了,以上资料按照一下操作即可获得
——将文章进行转发和评论,关注公众号【Java烤猪皮】,关注后继续后台回复领取口令“ 666 ”即可免费领文章取中所提供的资料。
腾讯、阿里、滴滴后台试题汇集总结 — (含答案)
面试:史上最全多线程序面试题!
最新阿里内推Java后端试题
JVM难学?那是因为你没有真正看完整这篇文章
关注作者微信公众号 — 《JAVA烤猪皮》
了解了更多java后端架构知识以及最新面试宝典
看完本文记得给作者点赞+在看哦~~~大家的支持,是作者来源不断出文的动力~
评论
