前言

SpringBoot方便之处在于它的各种自动配置,底层还是spring的基本配置,这样极大的节省了开发时间以及前期环境的准备 全自动有利有弊,弊端在于我们不知道它到底为我们做了哪些工作,我们还需要做哪些? 下面就来看看springboot的配置文件

1、全局配置文件:

application.properties
application.yml
*注意springboot会先加载yml文件后加载properties文件,所以会覆盖同名的yml

2、配置文件加载顺序:

可以看这篇博客 springboot 配置文件的加载顺序

/config --> / —> /resources/config --> /resources
*注意相同配置属性后面加载的会覆盖前面的属性,并且配置是互补的

3、yml基本语法

yml支持常量值,对象(包括map,set),数组

#即表示url属性值;
url: http://www.wolfcode.cn 
#即表示server.host属性的值; map也一样
server:
    host: http://www.wolfcode.cn 
map:
	key1: v1
	key2: v2    
#数组,即表示server为[a,b,c]
server:
    - 120.168.117.21
    - 120.168.117.22
    - 120.168.117.23
#常量
pi: 3.14   #定义一个数值3.14
hasChild: true  #定义一个boolean值
name: '你好YAML'   #定义一个字符串

参考博客1,YAML大小写敏感;
2,使用缩进代表层级关系;
3,缩进只能使用空格,不能使用TAB,不要求空格个数,只需要相同层级左对齐(一般2个或4个空格)

字符串可以不用引号标注,但是用单引号可以转移特殊字符




4、几个有关配置的注解

1、@ConfigurationProperties(prefix = “user”)
前提是对象也是spring容器中的
加载以user开头的属性 用于对象
这个注解可以把批量配置属性和对象进行封装,适用于批量
也可以在对象的属性上加上@value加载配置属性值

2、@value适合单个属性的加入,并且支持复杂语法

可以看这篇博客—点击

key="nb"

@Value("${key}") 取值
@Value("#{11*22}") 计算
@value(“nb”) 也可以直接放入值

3、@PropertySource(value={“person.properties”})加载指定配置文件
4、 @ImportResource(locations = {}) 加载Spring配置 但是springboot推荐配置类


5、激活配置文件,配置文件的类型

可能在开发过程中会用到好几套配置,springboot可以自主选择配置类型
可以创建 application-dev.properties application-prod.properties开发和生产环境配置
然后可以在application.properties中spring.profiles.active=dev这样就指定了用哪种配置文件


yml更简单 可以采用文档块的形式
注意:yml不能指定properfiles的配置文件

sring:
  profiles:
    active: a

---
server:
  port: 10000
spring:
  profiles: a
---
server:
  port: 8888

spring:
  profiles: b

yml的优先级会大于properties,所以如果同时存在这两种配置,因为properties是后加载的,所以此时yml就没有生效

也可以命令行来指定

--spring.profiles.active=dev
--server.port=8080 指定默认属性也可以


6、SpringBoot自动配置原理

扫描每个包下的META-INF/spring.properfiles文件,里面哪些配置了EnableConfiguration

@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

AutoConfigurationImportSelector点进来

@Override
	public String[] selectImports(AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return NO_IMPORTS;
		}
		AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
				.loadMetadata(this.beanClassLoader);
		AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
				autoConfigurationMetadata, annotationMetadata);
		return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());

AutoConfigurationMetadataLoader点进来

final class AutoConfigurationMetadataLoader {

	protected static final String PATH = "META-INF/"
			+ "spring-autoconfigure-metadata.properties";

下面以DataSourceProfiles为例
xxxxAutoConfigurations---->根据xxxProperfiles来判断是否添加组件

@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {

	private ClassLoader classLoader;

	/**
	 * Name of the datasource. Default to "testdb" when using an embedded database.
	 */
	private String name;

	/**
	 * Whether to generate a random datasource name.
	 */
	private boolean generateUniqueName;

	/**
@ConfigurationProperties(prefix = "spring.datasource") 应该很清楚了
@Configuration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ DataSourcePoolMetadataProvidersConfiguration.class,
		DataSourceInitializationConfiguration.class })
public class DataSourceAutoConfiguration {


总结

1、springboot的配置文件以及加载顺序
2、yml语法
3、自动配置原理以及相关注解

不足:对于很多注解还不懂