【Spring boot读书笔记】 SpringApplication-初始化阶段(2)

一、初始化–配置阶段

Spring Boot配置阶段位于构造器阶段和运行阶段之间,该阶段是可以选择的,主要用于调整或者补充构造阶段的状态、左右运行时行:

  • SpringApplication Setter方法为代表,用于调整SpringApplication的行为
  • 补充行为则以add*方法为主

此外通过Setter配置行为过于繁琐,因此,Spring Boot引入了SpringApplicationBuilder以提升API的便利性。

二、自定义SpringApplication

(1)调整SpringApplication配置

以banner关闭为例:

@SpringBootApplication
public class SimpleApplication2 {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MySpringConfiguration.class);
        // 设置关闭banner
        app.setBannerMode(Banner.Mode.CONSOLE);
        app.run(args);
    }
}

@EnableAutoConfiguration
class MySpringConfiguration {
}
(2)SpringApplication & SpringApplicationBuider 对比

// 1. SpringApplication setter 方式
class SimpleApplication3 {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MySpringConfiguration.class);

        springApplication.setBannerMode(Banner.Mode.CONSOLE);
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        springApplication.setAdditionalProfiles("prod");
        springApplication.setHeadless(true);
        springApplication.run(args);
    }
}


// 2. SpringApplicationBuider方式
class SimpleApplication4 {

    public static void main(String[] args) {
       new SpringApplicationBuilder(MySpringConfiguration.class)
               .bannerMode(Banner.Mode.CONSOLE)
               .web(WebApplicationType.NONE)
               .profiles("prod")
               .headless(true)
               .run(args);
    }
}

(3)SpringApplication配置源

SpringBoot 1.x 与SpringBoot 2.x对比,1.x中不区分主配置源,而2.x中通常将引导类作为主配置源,所以SpringApplication在前后版本中的使用方式是相同的,并且运行时也无法察觉其中变化,其主要原因在于1.x中的主配置以SpringApplication.sources属性存储:

// SpringBoot 1.x
public class SpringApplication {
	...
	private final Set<Object> sources = new LinkedHashSet<Object>();
	...

	public SpringApplication(Object... sources) {
		initialize(sources);
	}
	
	private void initialize(Object[] sources) {
		if (sources != null && sources.length > 0) {
			this.sources.addAll(Arrays.asList(sources));
		}
		...
	}
}


// SpringBoot 2.x 未移除sources 
public class SpringApplication {
	...
	// 主配置源
	private Set<Class<?>> primarySources;
	// 1.x 配置源
	private final Set<Object> sources = new LinkedHashSet<Object>();
	...

	public SpringApplication(Object... sources) {
		initialize(sources);
	}
	
	public SpringApplication(Class<?>... primarySources) {
		this(null, primarySources);
	}
	
	// 初始化主配置源
	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		...
	}

	// 初始化其他配置源
	private void prepareContext(ConfigurableApplicationContext context,
			ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
			ApplicationArguments applicationArguments, Banner printedBanner) {
		
		...
		
		// Load the sources
		Set<Object> sources = getAllSources();
		Assert.notEmpty(sources, "Sources must not be empty");
		load(context, sources.toArray(new Object[0]));
		listeners.contextLoaded(context);
	}
}


Spring Boot 2.x 支持配置源:

  • 主配置类
  • @Configuration Class
  • XML配置文件
  • package
(4)调整Spring Boot外部化配置

application.properties 是典型的外部化配置文件,实际上它可覆springApplication.setDefaultProperties(); 而反之springApplication.setDefaultProperties();也能通过spring.config.location 来定义application.properties的搜索路径;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值