---
title: "spring boot 项目笔记2一自定义配置文件的读取"
author: "resunly"
date: "2017-08-07T00:48:05Z"
tags: []
categories: ["编程语言"]
slug: "spring-boot-2"
draft: false
lastmod: "2017-08-07T00:45:24Z"
id: 2388488
url: "https://resunly.iteye.com/blog/2388488"
---

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

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

准备：

1.在application.yml文件种加入配置信息：

```
hank:
  testConfig: TestDriver
```

2.新建立配置文件 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
```

前面的准备 中我们知道

```
hank.testConfig 是在application.yml文件中，此文件为springboot
的默认配置文件
jdbc.driver 是在我们自定义的配置文件中的key
```

 

方法一：用springboot 自带的类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配置的都是系统自身的项，也就是不随系统环境改变而改变的配置项。一些易变的配置项我们还是自定义文件的比较好。我一般不会这么配置。

方法二：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文件的配置项

 

方法三：@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我们也能在读取自定义配置文件的同时读取到默认配置文件的信息。

以上就是三中读取配置文件的方式，可以看到要想读取自定义的配置文件，就必须要注解指定配置文件路径。