Springboot的应用配置参数主要在application配置文件中;
配置文件名称
默认的配置文件为application; 由配置项spring.config.name来决定,一般在项目开发中不会更改这个
源码(2.7.*版本):
public class StandardConfigDataLocationResolver implements ConfigDataLocationResolver<StandardConfigDataResource>, Ordered {
private static final String PREFIX = "resource:";
//配置文件的名称
static final String CONFIG_NAME_PROPERTY = "spring.config.name";
private static final String[] DEFAULT_CONFIG_NAMES = new String[]{"application"};
....
}
命令行参数可以使用spring.config.name 更改指定的配置文件名称;
java-jar xxx.jar -spring.config.name = fileName
配置文件扫描路径:
默认扫描路径为: optional:classpath:/;optional:classpath:/config/;
optional:file:./;optional:file:./config/;optional:file:./config/*/
其中optional表示可选;
源码(2.7.*版本):
class ConfigDataEnvironment {
/**
* Property used override the imported locations.
*/
static final String LOCATION_PROPERTY = "spring.config.location";
/**
* Property used to provide additional locations to import.
* 追加参数,不覆盖原有的默认值
*/
static final String ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
/**
* Property used to provide additional locations to import.
*/
static final String IMPORT_PROPERTY = "spring.config.import";
/**
忽略配置找不到的异常,不报错
* Property used to determine what action to take when a
* {@code ConfigDataNotFoundAction} is thrown.
* @see ConfigDataNotFoundAction
*/
static final String ON_NOT_FOUND_PROPERTY = "spring.config.on-not-found";
/**
* Default search locations used if not {@link #LOCATION_PROPERTY} is found.
*/
static final ConfigDataLocation[] DEFAULT_SEARCH_LOCATIONS;
static {
//启动时会在这些目录下查找spring.config.name的配置文件
List<ConfigDataLocation> locations = new ArrayList<>();
locations.add(ConfigDataLocation.of("optional:classpath:/;optional:classpath:/config/"));
locations.add(ConfigDataLocation.of("optional:file:./;optional:file:./config/;optional:file:./config/*/"));
DEFAULT_SEARCH_LOCATIONS = locations.toArray(new ConfigDataLocation[0]);
}
...
}
文件类型:
配置文件的类型分为properties和yml;
现在大多以yml为主,结构清晰,一个文件就可以配置多环境的配置;
将配置内容加载到spring环境中
1.直接在application中配置
2.使用spring.config.import导入配置(2.7.*)
spring.config.import= option:filePath1, option:filePath2
3.使用@PropertySource注解将文件加载进enviroment
properties文件
import lombok.Data;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
@SpringBootConfiguration
@PropertySource("classpath:propertiesconfig.properties")
@Data
@ConfigurationProperties("properties")
public class PropertyMapper {
String name;
String age;
}
yml文件
@SpringBootConfiguration
@PropertySource(value = "classpath:ymlconfig.yml",factory = YamlPropertiesSourceFactory.class)
@ConfigurationProperties(value = "ymlconfig",ignoreInvalidFields = true)
@Data
public class YmlMapper {
String name;
Integer age;
@DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate localDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
Date date;
}
yml解析配置
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
public class YamlPropertiesSourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
String resourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename());
List<PropertySource<?>> load = new YamlPropertySourceLoader().load(resourceName, resource.getResource());
return load.get(0);
}
}
获取配置值
1.使用Enviroment对象获取 environment.getProperty(“properties.name”)
2.@Value获取
3. @ConfigurationProperties绑定到对应的对象