springboot配置文件

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绑定到对应的对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值