赞
踩
1、运行 SpringApplication.run() 方法; 2、确定应用程序类型; 3、加载所有的初始化器(springboot自带初始化器,从从 META-INF/spring.factories 配置文件中加载; spring.factories文件里面,看到开头是 org.springframework.context.ApplicationContextInitializer 接口就是初始化器了.); 4、加载所有的监听器(加载监听器也是从 META-INF/spring.factories 配置文件中加载的,监听器加载的是实现了ApplicationListener接口的类.); 5、设置程序运行的主类(deduceMainApplicationClass(); 这个方法仅仅是找到main方法所在的类,为后面的扫包作准备,deduce是推断的意思,所以准确地说,这个方法作用是推断出主方法所在的类); 6、开启计时器(计算springboot启动花了多长时间;程序运行到这里,就已经进入了run方法的主体了,第一步调用的run方法是静态方法,那个时候还没实例化SpringApplication对象,现在调用的run方法是非静态的,是需要实例化后才可以调用的,进来后首先会开启计时器); // 实例化计时器 StopWatch stopWatch = new StopWatch(); // 开始计时 stopWatch.start(); 7、将java.awt.headless设置为true(表示运行在服务器端,在没有显示器?和鼠标键盘的模式下照样可以工作,模拟输入输出设备功能;即使没有检测到显示器,也允许其启动.对于服务器来说,是不需要显示器的,所以要这样设置.); 8、获取并启用监听器(通过监听器来实现初始化的的基本操作做了2件事情: 1、创建所有Spring运行监听器并发布应用启动事件;2、启用监听器;); 9、设置应用程序参数(将执行run方法时传入的参数封装成一个对象;这个参数是从哪来的呢?其实就是main方法里面执行静态run方法传入的参数。); 10、准备环境变量(准备环境变量,包含系统属性和用户配置的属性,执行的代码块在 prepareEnvironment 方法内。); 11、忽略bean信息(这个方法configureIgnoreBeanInfo() 这个方法是将 spring.beaninfo.ignore 的默认值设为true,意思是跳过beanInfo的搜索,其设置默认值的原理和第7步一样); 12、打印banner信息(他在哪里打印的呢?他在SpringBootBanner.java里面打印的,这个类实现了Banner接口;banner信息是直接在代码里面写死的;如果想要改成自定义banner信息,该怎么办呢?只需要在resources目录下添加一个 banner.txt 的文件即可); 13、创建应用程序的上下文(实例化应用程序的上下文, 调用 createApplicationContext() 方法,这里就是用反射创建对象); 14、实例化异常报告器(异常报告器是用来捕捉全局异常使用的,当springboot应用程序在发生异常时,异常报告器会将其捕捉并做相应处理,在spring.factories 文件里配置了默认的异常报告器。需要注意的是,这个异常报告器只会捕获启动过程抛出的异常,如果是在启动完成后,在用户请求时报错,异常报告器不会捕获请求中出现的异常。); 15、准备上下文环境; 15.1、实例化单例的beanName生成器 在 postProcessApplicationContext(context); 方法里面。使用单例模式创建 了BeanNameGenerator 对象,其实就是beanName生成器,用来生成bean对象的名称; 15.2、执行初始化方法 初始化方法有哪些呢?还记得第3步里面加载的初始化器嘛?其实是执行第3步加载出来的所有初始化器,实现了ApplicationContextInitializer 接口的类; 15.3、将启动参数注册到容器中 这里将启动参数以单例的模式注册到容器中,是为了以后方便拿来使用,参数的beanName 为 :springApplicationArguments; 16、刷新上下文(刷新上下文已经是spring的范畴了,自动装配和启动 tomcat就是在这个方法里面完成的); 17、刷新上下文后置处理(afterRefresh方法是启动后的一些处理,留给用户扩展使用,目前这个方法里面是空的。); 18、结束计时器(springboot其实就已经完成了,计时器会打印启动springboot的时长。);

package com.spring;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class App {
public static void main(String[] args) {
// 启动springboot
ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
}
}
/**
* Static helper that can be used to run a {@link SpringApplication} from the * specified sources using default settings and user supplied arguments. * @param primarySources the primary sources to load * @param args the application arguments (usually passed from a Java main method) * @return the running {@link ApplicationContext} */
public static ConfigurableApplicationContext run(Class<?>[] primarySources,String[] args) {
return new SpringApplication(primarySources).run(args);
}



package com.spring.application;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
/** * 自定义的初始化器 */
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
System.out.println("我是初始化的 MyApplicationContextInitializer...");
}
}
org.springframework.context.ApplicationContextInitializer=\
com.spring.application.MyApplicationContextInitializer




// 实例化计时器
StopWatch stopWatch = new StopWatch();
// 开始计时
stopWatch.start();

private void configureHeadlessProperty() {
System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
}


public DefaultApplicationArguments(String[] args) {
Assert.notNull(args, "Args must not be null");
this.source = new Source(args);
this.args = args;
}



private void configureIgnoreBeanInfo(ConfigurableEnvironment environment) {
if (System.getProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME) == null) {
Boolean ignore = environment.getProperty("spring.beaninfo.ignore",Boolean.class, Boolean.TRUE);
System.setProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME,ignore.toString());
}
}
spring.beaninfo.ignore=false


_ _
(_) | |
_ _ _____ ___ _ __ __| | ___ _ __ __ _
| | | |/ _ \ \/ / | '_ \ / _` |/ _ \| '_ \ / _` |
| |_| | __/> <| | | | | (_| | (_) | | | | (_| |
\__, |\___/_/\_\_|_| |_|\__,_|\___/|_| |_|\__, |
__/ | __/ |
|___/ |___/
:: yexindong::





package com.spring.application; import org.springframework.boot.SpringBootExceptionReporter; import org.springframework.context.ConfigurableApplicationContext; public class MyExceptionReporter implements SpringBootExceptionReporter { private ConfigurableApplicationContext context; // 必须要有一个有参的构造函数,否则启动会报错 MyExceptionReporter(ConfigurableApplicationContext context) { this.context = context; } @Override public boolean reportException(Throwable failure) { System.out.println("进入异常报告器"); failure.printStackTrace(); // 返回false会打印详细springboot错误信息,返回true则只打印异常信息 return false; } }
# Error Reporters 异常报告器
org.springframework.boot.SpringBootExceptionReporter=\
com.spring.application.MyExceptionReporter

server:
port: 80828888



/** * Called after the context has been refreshed. * @param context the application context * @param args the application arguments */
protected void afterRefresh(ConfigurableApplicationContext context,ApplicationArguments args) { }



package com.spring.init; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * 自定义run方法的2种方式 */ @Component public class MyRunner implements ApplicationRunner, CommandLineRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(" 我是自定义的run方法1,实现 ApplicationRunner 接口既可运行" ); } @Override public void run(String... args) throws Exception { System.out.println(" 我是自定义的run方法2,实现 CommandLineRunner 接口既可运行" ); } }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。