在spring boot种自定义配置文件的读取很方便,不用在写propert的读取类来读取配置文件信息。

下面是我试过的读取springboot读取配置文件的几种方法:

准备

  1. 在application.yml文件种加入配置信息:
    1. hank:
    2. testConfig: TestDriver
  1. 新建立配置文件 dbConfig.properties
    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/test
    3. jdbc.username=root
    4. jdbc.password=root
    5. org.hank.testconfig=testConfig

配置文件读取

1.Environment读取配置文件

  1. @Autowired
  2. private Environment env;
  3. @Test
  4. public void testConfigDefault() {
  5. logger.info("Environment get default properties:" + env.getProperty("hank.testConfig"));
  6. logger.info("Environment get self properties:" + env.getProperty("jdbc.driver"));
  7. }

测试结果

  1. Environment get default properties:TestDriver
  2. Environment get self properties:null

结论:也就是说Environment自带的只能读取默认的配置文件里面的配置信息,自定义的配置文件在Environment是读取不了的。况且在application.yml配置的都是系统自身的项,也就是不随系统环境改变而改变的配置项。一些易变的配置项我们还是自定义文件的比较好。我一般不会这么配置。

2.Configurable方式读取配置文件

建立类ConfigDefault 注解@Configurable

  1. @Component
  2. @Configurable
  3. public class ConfigDefault {
  4. @Value("${hank.testConfig}")
  5. private String hankConfig;
  6. @Value("${org.hank.testconfig}")
  7. private String selfConfig;
  8. getter and setter....
  9. }

测试:

  1. @Autowired
  2. private ConfigDefault configDefault;
  3. @Test
  4. public void testConfigDefault() {
  5. logger.info("defualt config--hank.testConfig:" + configDefault.getHankConfig());
  6. logger.info("self config--org.hank.testconfig:" + configDefault.getSelfConfig());
  7. }

测试结果:直接报错,Could not resolve placeholder ‘org.hank.testconfig’ in value “${org.hank.testconfig}”
也就说application.yml文件中没有org.hank.testconfig这配置项,所以在类上加@Configurable也是默认只读取application.yml文件的配置项

3.@PropertySource注解方式读取配置文件

新建model类:

  1. @Component
  2. @PropertySource("classpath:dbConfig.properties")
  3. public class DataBaseConfig {
  4. @Value("${jdbc.driver}")
  5. private String driver;
  6. @Value("${jdbc.url}")
  7. private String url;
  8. @Value("${jdbc.username}")
  9. private String userName;
  10. @Value("${jdbc.password}")
  11. private String password;
  12. @Value("${hank.testConfig}") //测试默认配置文件
  13. private String hankConfig;
  14. getter and setter...
  15. }

测试:

  1. @Autowired
  2. private DataBaseConfig config;
  3. @Test
  4. public void testGetDataBaseConfig() {
  5. logger.info("self Config Driver:" + config.getDriver());
  6. logger.info("default config hank.testConfig:" + config.getHankConfig());
  7. }

测试结果:

  1. self Config Driver:com.mysql.jdbc.Driver
  2. default config hank.testConfig:TestDriver

可以看出连同默认配置的信息也读取到了

结论:用@PropertySource(“classpath:dbConfig.properties”)
指定自定义配置文件路径就可以读取到自定义的配置文件信息,而对于默认配置文件application.yml我们也能在读取自定义配置文件的同时读取到默认配置文件的信息。
以上就是三中读取配置文件的方式,可以看到要想读取自定义的配置文件,就必须要注解指定配置文件路径。

4.使用@ConfigurationProperties读取配置文件

特別注意:

  • 需要引入spring-boot-configuration-processor依赖
  • 这种方法适合读取大量配置属性,
  • @ConfigurationProperties注解的实体类必须有get/set方法

新建一个实体类:

  1. @ConfigurationProperties(prefix = "test", locations = "classpath:test.properties")
  2. @Data
  3. public class Test {
  4. private String one;
  5. private String two;
  6. private String three;
  7. private String four;
  8. private String five;
  9. }

配置文件test.properties

  1. test.one=one
  2. test.two=two
  3. test.three=three
  4. test.four=four
  5. test.five=five

测试

  1. @RestController
  2. @EnableConfigurationProperties(Test.class)
  3. public class HelloController {
  4. @Resource
  5. private Test test;
  6. @RequestMapping("/hello")
  7. public String hello() {
  8. return test.toString();
  9. }
  10. }

結果

  1. Test{one='one', two='two', three='three', four='four', five='five'}