Spring Boot 2关于配置文件的使用

it2022-05-05  158

Spring Boot 2关于配置文件的使用

配置文件使用通过@Value取值通过@ConfigurationProperties通过Spring的Environment来获取通过@PropertySource引入其他properties文件通过@ImportResource引入Spring配置通过启动参数spring.config.location来处理如下目录结构,将不同环境的配置文件放在不同目录:通过启动参数来控制:通过@PropertySource需要指定路径的要动态拼接路径

配置文件使用

properties文件yml文件 当前例子全部使用yml配置文件,区分不同环境的配置

application-dev.yml application-devDb.yml application-devRedis.yml

application-test.yml application-testDb.yml application-testRedis.yml

application-pre.yml application-preDb.yml application-preRedis.yml

application-prod.yml application-prodDb.yml application-prodRedis.yml

在yml文件中引入其他yml文件

spring: profiles: include: devDb,devRedis

通过启动参数-Dspring.profiles.active=test来决定配置文件的使用

通过@Value取值

对于单个值的获取很方便,支持SpEL 表达式

@Value("${datasource.url}") private String dbUrl;

通过@ConfigurationProperties

这个主要针对有特殊意义的多个值放在一起,在使用的时候可以直接当做一个Bean来使用

@Component @ConfigurationProperties(prefix = "datasource") public class DbProperties implements Serializable { private String type; private String url; private String username; private String password; private String driverClassName; //省略get,set方法 }

通过Spring的Environment来获取

public static String getValue(String key) { if (StringUtils.isNotBlank(key)) { Environment environment = getEnvironment(); if (environment != null) { return environment.getProperty(key); } } return null; }

通过@PropertySource引入其他properties文件

这个只能针对properties,

@Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"classpath:person.properties"}, encoding = "UTF-8") public class OtherProperties implements Serializable { private String name; private Integer age; //省略get,set方法 }

通过@ImportResource引入Spring配置

首先需要配置相关的Bean,然后再入口类上面引用

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="testComponent" class="org.ghost.springboot.demo.common.component.TestComponent" init-method="init" destroy-method="destory"> <property name="name" value="测试Hello"/> </bean> </beans> @ImportResource(value = {"classpath:other.xml"}) @SpringBootApplication(scanBasePackages = "org.ghost.springboot.demo") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

通过启动参数spring.config.location来处理

如下目录结构,将不同环境的配置文件放在不同目录:

dev application.yamlperson.propertieslogback.xml test application.yamlperson.propertieslogback.xml prod application.yamlperson.propertieslogback.xml

通过启动参数来控制:

-Dspring.profiles.active=dev -Dspring.config.location=classpath:/env/dev/-Dspring.profiles.active=test -Dspring.config.location=classpath:/env/test/-Dspring.profiles.active=prod -Dspring.config.location=classpath:/env/prod/

通过@PropertySource需要指定路径的要动态拼接路径

@Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"${spring.config.location}person.properties"}, encoding = "UTF-8") public class PersonProperties implements Serializable { // }

源码位置: https://gitee.com/ceclar123/spring-boot-demo/tree/master/ch07 https://gitee.com/ceclar123/spring-boot-demo/tree/master/ch08


最新回复(0)