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

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

Spring Boot入门+深入(八)-启动配置原理 一、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  Collection getSpringFactoriesInstances(Class type) {
	return getSpringFactoriesInstances(type, new Class[] {});
}
private  Collection getSpringFactoriesInstances(Class type,
		Class[] parameterTypes, Object... args) {
	ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
	// Use names and ensure unique to protect against duplicates
	Set names = new linkedHashSet(
			SpringFactoriesLoader.loadFactoryNames(type, classLoader));
	List instances = createSpringFactoriesInstances(type, parameterTypes,
			classLoader, args, names);
	AnnotationAwareOrderComparator.sort(instances);
	return instances;
}

SpringFactoriesLoader.loadFactoryNames(type, classLoader));

	public static List loadFactoryNames(Class factoryClass, ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		try {
			Enumeration urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			List result = new ArrayList();
			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> initializers) {
	this.initializers = new ArrayList>();
	this.initializers.addAll(initializers);
}

this.initializers

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

public void setListeners(Collection> listeners) {
	this.listeners = new ArrayList>();
	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 {
    @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.jarmeta-INFspring.factories有启动时需要加载的 EnableAutoConfiguration组件配置

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

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

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

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

参考之前文章Spring Boot入门+深入(二)--六、自动配置原理:Spring Boot入门+深入(二)_杀神lwz的博客-CSDN博客

Spring Boot入门+深入(一):Spring Boot入门+深入(一)_杀神lwz的博客-CSDN博客

Spring Boot入门+深入(九):Spring Boot入门+深入(九)-自定义starters_杀神lwz的博客-CSDN博客

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5481348.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存