今天就跟大家聊聊有关springboot中application.yml的文件配置是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
10年积累的成都网站建设、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有曲水免费网站建设让你可以放心的选择与我们合作。
springboot配置文件
springboot最简便的地方,一是开箱即用,二是配置简单,配置文件路径一般在/src/main/resources 下,主要配置有两种形式,一种是properties文件,一种是springboot官方推荐的yml后缀的文件
一、properties/yml文件配置spring
使用properties文件springboot配置
#配置内置tomcat启动端口 server.port=8080 #给程序起个名字 spring.application.name=boot-helloworld
properties文件作为众多配置文件中的佼佼者,使用比较方便,格式简单,等号左边为属性,右边为值,可以说是简单方便
使用yml文件作为springboot配置
#配置内置tomcat启动端口 server: port: 8080 #应用名称 spring: application: name: boot-helloworld
yml文件中,有着严格的层级关系,以空格或者Tab缩进表示,英文冒号结尾,末尾层级最后空至少一个英文空格,然后填写该属性的值,优点在于,层级表示可以简单的连着写多个值,例如:
spring: application: name: boot-helloworld #redis连接池配置, 可以换行保持缩进,连着写 redis: host: localhost port: 6379 password: password timeOut: 5000 maxIdle: 50 maxWaitMillis: 5000 maxTotal: 500
二、springboot常用配置项
以下摘自 spring-boot-autoconfiguration.jar中的spring-configuration-metadata.json,另外,springboot默认配置都在此包中的spring-autoconfigure-metadata.properties文件中有指定,有需要的同学可以去翻阅,不同springboot版本,默认的属性有区别
指定项目启动端口
server: port: 8081
给项目指定名称
spring: application: name: boot-helloworld
日志级别
#默认情况下springboot使用Logback作为日志框架 #logging.level开头,指定一个依赖的groupId然后,指定日志级别 logging: level: org.springframeword.web: debug
多个环境下指定启动使用的配置环境
#只需要在application.properties中设置spring.profiles.active=prod来指定活动的profile即可 #如下表示采用application-test.yml #默认使用default(application.yml),这样可以区分开发、线上、测试,preview等环境 spring: profiles: active: test
指定项目路径
#在所有接口访问路径之前需要加/boot server: servlet: context-path: /boot
三、基于@Value注解读取自定义属性
以下代码仅供演示
1: User.java
/**
* 当作一个配置项,简单处理
* @author Calvin
* @date 2019/07/24
*/
public class User {
/**
* ID
*/
private String id;
/**
* 名字
*/
private String userName;
/**
* 年龄
*/
private Integer age;
/**
* 性别
*/
private String gender;
/**
* 所使用的操作系统
*/
private String systemName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getSystemName() {
return systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", systemName='" + systemName + '\'' +
'}';
}
}2: application.yml
spring: application: name: boot-helloworld logging: level: org.springframeword.web: debug server: servlet: context-path: /boot # admin年龄的值配置 admin: age: 20
3:ValueController.java
/**
* 测试@Value注解的各种方式
* @author Calvin
* @date 2019/07/24
*/
@RestController
@RequestMapping("/value")
public class ValueController {
/**
* ID 采用表达式注解
*/
@Value("#{ T(java.util.UUID).randomUUID()}")
private String adminId;
/**
* name 采用值注解获取一个UUID
*/
@Value("Calvin")
private String adminName;
/**
* gender 给定一个默认值,如果没有定义,就采用默认值
*/
@Value("${admin.gender: 男}")
private String adminGender;
/**
* 注入yml文件中的admin.age
*/
@Value("${admin.age}")
private Integer adminAge;
/**
* 采用表达式获取系统变量
*/
@Value("#{systemProperties['os.name']}")
private String systemName;
/**
* 获取用户信息
* @return
*/
@GetMapping("/getUserInfo")
public User getAdminInfo(){
User admin = new User();
admin.setId(adminId);
admin.setUserName(adminName);
admin.setAge(adminAge);
admin.setGender(adminGender);
admin.setSystemName(systemName);
return admin;
}
}4:调用结果
springboot配置文件不仅可以使用@Value注解, 还有很多好玩的方式,另一个可以使用@ConfigurationProperties去注入到某个Bean中,具体方法此处不做调研
四、yml配置文件加载源码解读
源码解读非常不容易,笔者也是参考了网上很多资源,才撰写出这部分,希望大家多多指教,此处就不画图了,画图功底不好
1: 调用链
BootApplication.main()
//main方法,程序入口 SpringApplication.run(BootApplication.class, args);
SpringApplication.run():1202
//实例化SpringApplication并且调用run方法 return new SpringApplication(primarySources).run(args);
SpringApplication.run():304
//此方法中实例化Environment ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
SpringApplication.prepareEnvironment():338
//因为我是Web程序,用的是spring-boot-starter-web依赖,所以是StandardServletEnvironment
private ConfigurableEnvironment getOrCreateEnvironment() {
//省略部分代码
return new StandardServletEnvironment();
}
//因为StandardServletEnviroment extends AbstractEnvironment
//而AbstractEnvironment构造器中中调用了customizePropertySources()方法
public AbstractEnvironment() {
//模板方法,大家都懂得
customizePropertySources(this.propertySources);
}StandardServletEnvironment.customizePropertySources():54
protected void customizePropertySources(MutablePropertySources propertySources) {
//将servletConfigInitParams添加进PropertySource中
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
//将servletContextInitParams添加进PropertySource中
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
}
//调用AbstractEnvironment.customizePropertySources()方法
super.customizePropertySources(propertySources);
}AbstractEnvironment.customizePropertySources():77
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
//添加systemProperties, 添加的是System.getProperties(),是一个Native方法,主要属性是
//java.runtime.name | java.vm.version等系统配置的属性
propertySources.addLast(
new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
//添加systemEnvironment, 也就是系统环境变量, 其中包含JAVA_HOME等属性
propertySources.addLast(
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}至此结束,ConfigurableEnvironment对象中添加了四个propertySource,分别为:
[servletConfigInitParams, servletContextInitParams , systemProperties, systemEnvironment]
看完 SpringApplication.prepareEnvironment():338所作的事情, 接着看listeners.environmentPrepared(environment):340,这也是yml文件配置的入口
SpringApplicationRunListeners.environmentPremared
public void environmentPrepared(ConfigurableEnvironment environment) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
}这个方法中加载了很多个Listener,每个的调用链也非常深,这里简短点,
SpringApplicationRunListeners.java
environmentPrepared-->
SimpleApplcationEventMulticaster.java
multicastEvent():126&131-->
invokeListener():159-->
doInvokeListener(listener, event) -->
CofigFileApplicationListener.java
onApplicationEvent():-->
addPostProcessors();-->
new Loader(environment, resourceLoader).load():202-->
load(String location, String name, Profile profile, DocumentFilterFactory filterFactory,DocumentConsumer consumer):429-->
前方高能,笔者跟代码跟的非常之辛苦,废话少说,ConfigFileApplicationListener.java中核心代码
ConfigFileApplicationListener.onApplicationEvent()
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}ConfigFileApplicationListener.load()
private void load(String location, String name, Profile profile, DocumentFilterFactory filterFactory,
DocumentConsumer consumer) {
if (!StringUtils.hasText(name)) {
for (PropertySourceLoader loader : this.propertySourceLoaders) {
if (canLoadFileExtension(loader, location)) {
load(loader, location, profile, filterFactory.getDocumentFilter(profile), consumer);
return;
}
}
}
Set processed = new HashSet<>();
//遍历propertySourceLoaders
for (PropertySourceLoader loader : this.propertySourceLoaders) {
for (String fileExtension : loader.getFileExtensions()) {
if (processed.add(fileExtension)) {
//寻找配置文件
loadForFileExtension(loader, location + name, "." + fileExtension, profile, filterFactory,
consumer);
}
}
}
} this.propertySourceLoaders中有两个类,都是PropertySourceLoader的实现类,分别是
[ org.springframework.boot.env.PropertiesPropertySourceLoader, org.springframework.boot.env.YamlPropertySourceLoader ]
PropertiesPropertySourceLoader.java
/** **
YamlPropertySourceLoader.java
/** **


咨询
建站咨询
