Spring Boot入门+深入(八)-启动配置原理

目录

一、Spring Boot启动配置原理

1.启动原理

1.创建SpringApplication对象

2.运行run方法

3.事件监听机制

2.自动配置


一、Spring Boot启动配置原理

1.启动原理

SpringApplication.run(主程序类启动)

public static ConfigurableApplicationContext run(Object source, String... args) {
	return run(new Object[] { source }, args);
}
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
	return new SpringApplication(sources).run(args);
}
public SpringApplication(Object... sources) {
	initialize(sources);
}

1.创建SpringApplication对象

initialize

@SuppressWarnings({ "unchecked", "rawtypes" })
private void initialize(Object[] sources) {
    //保存主配置类
	if (sources != null && sources.length > 0) {
		this.sources.addAll(Arrays.asList(sources));
	}
    //判断当前是否一个web应用
	this.webEnvironment = deduceWebEnvironment();
    //从类路径下找到META‐INF/spring.factories配置的所有ApplicationContextInitializer然后保存起来
	setInitializers((Collection) getSpringFactoriesInstances(
			ApplicationContextInitializer.class));
    //从类路径下找到META‐INF/spring.factories配置的所有ApplicationListener
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    //从多个配置类中找到有main方法的主配置类
	this.mainApplicationClass = deduceMainApplicationClass();
}

解析:setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));

private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type) {
	return getSpringFactoriesInstances(type, new Class<?>[] {});
}
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,
		Class<?>[] parameterTypes, Object... args) {
	ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
	// Use names and ensure unique to protect against duplicates
	Set<String> names = new LinkedHashSet<String>(
			SpringFactoriesLoader.loadFactoryNames(type, classLoader));
	List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
			classLoader, args, names);
	AnnotationAwareOrderComparator.sort(instances);
	return instances;
}

SpringFactoriesLoader.loadFactoryNames(type, classLoader));

	public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		try {
			Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			List<String> result = new ArrayList<String>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
				String factoryClassNames = properties.getProperty(factoryClassName);
				result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
			}
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
					"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

FACTORIES_RESOURCE_LOCATION

public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

-------------------解析完setInitializers-----------------

解析完:setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));

public void setInitializers(
		Collection<? extends ApplicationContextInitializer<?>> initializers) {
	this.initializers = new ArrayList<ApplicationContextInitializer<?>>();
	this.initializers.addAll(initializers);
}

this.initializers

解析完: setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
	this.listeners = new ArrayList<ApplicationListener<?>>();
	this.listeners.addAll(listeners);
}

this.listeners 

解析: this.mainApplicationClass = deduceMainApplicationClass();

    private Class<?> deduceMainApplicationClass() {
		try {
			StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
			for (StackTraceElement stackTraceElement : stackTrace) {
				if ("main".equals(stackTraceElement.getMethodName())) {
					return Class.forName(stackTraceElement.getClassName());
				}
			}
		}
		catch (ClassNotFoundException ex) {
			// Swallow and continue
		}
		return null;
	}

2.运行run方法

public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
	return new SpringApplication(sources).run(args);
}

new SpringApplication(sources).run(args)

	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		FailureAnalyzers analyzers = null;
		configureHeadlessProperty();
        //获取SpringApplicationRunListeners;从类路径下META‐INF/spring.factories
		SpringApplicationRunListeners listeners = getRunListeners(args);
        //回调所有的SpringApplicationRunListener.starting()方法
		listeners.starting();
		try {
            //封装命令行参数
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
            //准备环境
            //创建环境完成后回调SpringApplicationRunListener.environmentPrepared()表示环境准备完成
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
            //在控制台上打印图标
			Banner printedBanner = printBanner(environment);
            //创建ApplicationContext;决定创建web的ioc还是普通的ioc
			context = createApplicationContext();
            //失败信息
			analyzers = new FailureAnalyzers(context);
            //准备上下文环境;将environment保存到ioc中;而且applyInitializers();
            //applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
            //回调所有的SpringApplicationRunListener的contextPrepared();
            //prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded();
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
            //刷新容器;ioc容器初始化(bean加载..)(如果是web应用还会创建嵌入式的Tomcat);
            //扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
			refreshContext(context);
            //从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
            //ApplicationRunner先回调,CommandLineRunner再回调
			afterRefresh(context, applicationArguments);
            //所有的SpringApplicationRunListener回调finished方法
			listeners.finished(context, null);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass)
						.logStarted(getApplicationLog(), stopWatch);
			}
            //整个SpringBoot应用启动完成以后返回启动的ioc容器;
			return context;
		}
		catch (Throwable ex) {
			handleRunFailure(context, listeners, analyzers, ex);
			throw new IllegalStateException(ex);
		}
	}

在控制台上打印图标Banner printedBanner = printBanner(environment);如下

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

3.事件监听机制

配置在META-INF/spring.factories

        1.ApplicationContextInitializer

        2.SpringApplicationRunListener

只需要放在ioc容器中

        3.ApplicationRunner

        4.CommandLineRunner

自己创建实现监听,启动效果查看上面运行run方法的启动时机。

HelloApplicationContextInitializer 

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("ApplicationContextInitializer...initialize..."+applicationContext);
    }
}

HelloSpringApplicationRunListener 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {

    //必须有的构造器
    public HelloSpringApplicationRunListener(SpringApplication application, String[] args){

    }

    @Override
    public void starting() {
        System.out.println("SpringApplicationRunListener...starting...");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        Object o = environment.getSystemProperties().get("os.name");
        System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextPrepared...");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextLoaded...");
    }

    @Override
    public void finished(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("SpringApplicationRunListener...finished...");
    }
}

HelloApplicationRunner 

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class HelloApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner...run....");
    }
}

HelloCommandLineRunner 

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class HelloCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));
    }
}

resources-->META-INF-->spring.factories 仿照别的配置文件配置

org.springframework.context.ApplicationContextInitializer=\
com.springboot.listener.HelloApplicationContextInitializer

org.springframework.boot.SpringApplicationRunListener=\
com.springboot.listener.HelloSpringApplicationRunListener

启动项目:输出先后顺序

SpringApplicationRunListener...starting...
SpringApplicationRunListener...environmentPrepared..Windows 10

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

...
SpringApplicationRunListener...contextPrepared...
...
SpringApplicationRunListener...contextLoaded...
...
ApplicationRunner...run....
CommandLineRunner...run...[]
SpringApplicationRunListener...finished...
...

2.自动配置

Spring Boot启动扫描所有jar包的META-INF/spring.factories中配置的 EnableAutoConfiguration组件

spring-boot-autoconfigure.jar\META-INF\spring.factories有启动时需要加载的 EnableAutoConfiguration组件配置

配置文件中使用debug=true可以观看到当前启用的自动配置的信息

自动配置会为容器中添加大量组件

Spring Boot在做任何功能都需要从容器中获取这个功能的组件

Spring Boot 总是遵循一个标准;容器中有我们自己配置的组件就用我们配置的,没有就用自动配 置默认注册进来的组件;

参考之前文章Spring Boot入门+深入(二)--六、自动配置原理:Spring Boot入门+深入(二)

Spring Boot入门+深入(一):Spring Boot入门+深入(一)

Spring Boot入门+深入(九):Spring Boot入门+深入(九)-自定义starters

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀神lwz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值