在spring boot种自定义配置文件的读取很方便,不用在写propert的读取类来读取配置文件信息。
下面是我试过的读取springboot读取配置文件的几种方法:
准备
- 在application.yml文件种加入配置信息:
hank:
testConfig: TestDriver
- 新建立配置文件 dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
org.hank.testconfig=testConfig
配置文件读取
1.Environment读取配置文件
@Autowired
private Environment env;
@Test
public void testConfigDefault() {
logger.info("Environment get default properties:" + env.getProperty("hank.testConfig"));
logger.info("Environment get self properties:" + env.getProperty("jdbc.driver"));
}
测试结果
Environment get default properties:TestDriver
Environment get self properties:null
结论:也就是说Environment自带的只能读取默认的配置文件里面的配置信息,自定义的配置文件在Environment是读取不了的。况且在application.yml配置的都是系统自身的项,也就是不随系统环境改变而改变的配置项。一些易变的配置项我们还是自定义文件的比较好。我一般不会这么配置。
2.Configurable方式读取配置文件
建立类ConfigDefault 注解@Configurable
@Component
@Configurable
public class ConfigDefault {
@Value("${hank.testConfig}")
private String hankConfig;
@Value("${org.hank.testconfig}")
private String selfConfig;
getter and setter....
}
测试:
@Autowired
private ConfigDefault configDefault;
@Test
public void testConfigDefault() {
logger.info("defualt config--hank.testConfig:" + configDefault.getHankConfig());
logger.info("self config--org.hank.testconfig:" + configDefault.getSelfConfig());
}
测试结果:直接报错,Could not resolve placeholder ‘org.hank.testconfig’ in value “${org.hank.testconfig}”
也就说application.yml文件中没有org.hank.testconfig这配置项,所以在类上加@Configurable也是默认只读取application.yml文件的配置项
3.@PropertySource注解方式读取配置文件
新建model类:
@Component
@PropertySource("classpath:dbConfig.properties")
public class DataBaseConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Value("${hank.testConfig}") //测试默认配置文件
private String hankConfig;
getter and setter...
}
测试:
@Autowired
private DataBaseConfig config;
@Test
public void testGetDataBaseConfig() {
logger.info("self Config Driver:" + config.getDriver());
logger.info("default config hank.testConfig:" + config.getHankConfig());
}
测试结果:
self Config Driver:com.mysql.jdbc.Driver
default config hank.testConfig:TestDriver
可以看出连同默认配置的信息也读取到了
结论:用@PropertySource(“classpath:dbConfig.properties”)
指定自定义配置文件路径就可以读取到自定义的配置文件信息,而对于默认配置文件application.yml我们也能在读取自定义配置文件的同时读取到默认配置文件的信息。
以上就是三中读取配置文件的方式,可以看到要想读取自定义的配置文件,就必须要注解指定配置文件路径。
4.使用@ConfigurationProperties读取配置文件
特別注意:
- 需要引入spring-boot-configuration-processor依赖
- 这种方法适合读取大量配置属性,
- @ConfigurationProperties注解的实体类必须有get/set方法
新建一个实体类:
@ConfigurationProperties(prefix = "test", locations = "classpath:test.properties")
@Data
public class Test {
private String one;
private String two;
private String three;
private String four;
private String five;
}
配置文件test.properties
test.one=one
test.two=two
test.three=three
test.four=four
test.five=five
测试
@RestController
@EnableConfigurationProperties(Test.class)
public class HelloController {
@Resource
private Test test;
@RequestMapping("/hello")
public String hello() {
return test.toString();
}
}
結果
Test{one='one', two='two', three='three', four='four', five='five'}