美文网首页
教会你如何将自定义的配置文件注入到SpringBoot

教会你如何将自定义的配置文件注入到SpringBoot

作者: Kyriez7 | 来源:发表于2022-09-23 09:38 被阅读0次

    一、简介

    在实际的项目开发过程中,我们经常需要将某些变量从代码里面抽离出来,放在配置文件里面,以便更加统一、灵活的管理服务配置信息。比如,数据库、eureka、zookeeper、redis、mq、kafka 等服务组件的连接参数配置,还有我们自定义的项目参数配置变量。

    当然,实际上根据当前的业务需求,我们往往会自定义参数,然后注入到代码里面去,以便灵活使用!

    SpringBoot 项目在启用时,首先会默认加载bootstrap.properties或者bootstrap.yml这两个配置文件(这两个优先级最高);接着会加载application.properties或application.yml;如果何配置了spring.profiles这个变量,同时还会加载对应的application-{profile}.properties或者application-{profile}.yml文件,profile为对应的环境变量,比如dev,如果没有配置,则会加载profile=default的配置文件。

    虽然说配置项都写在同一个配置文件没有问题,但是很多时候我们仍然希望能分开写,这样会比较清晰,比如zookeeper的配置写在zookeeper.properties,数据库相关的配置写在datasource.properties等等,因此就需要设置加载外部配置文件!

    具体该如何实现呢,我们一起来看看!

    二、代码实践

    2.1、通过@value注解实现参数加载

    当我们想要在某个类里面注入某个变量,通过@value注解就可以简单实现参数的注入!

    例如application.properties文件里,配置一个config.name的变量key,值为zhangsan

    <pre class="prettyprint hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//参数定义
    config.name=zhangsan</pre>

    然后在对应的类里面,通过参数@value注入即可!

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RestController
    public class HelloController {

    @Value("${config.name}")
    private String config;
    
    @GetMapping("config")
    public String config(){
        return JSON.toJSONString(config);
    }
    

    }</pre>

    使用@value注解注入配置,通常情况下有个要求就是,注解里面的变量,必须在application.properties文件里面事先定义好,否则启动报错!

    当然,如果我们不想让它抱错,我们可以给它一个缺省值xxx,比如:

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Value("${config.name:xxx}")
    private String config;</pre>

    这样,SpringBoot 项目在启用时不会报错!

    2.2、通过@ConfigurationProperties注解实现参数加载

    某些场景下,@value注解并不能满足我们所有的需求,比如参数配置的数据类型是一个对象或者数组,这个时候才用@ConfigurationProperties会是一个比较好的选择!

    配置一个对象类型的参数

    例如在application.properties文件里,当我们想配置一个对象类型的参数,我们可以这样操作!

    <pre class="prettyprint hljs delphi" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//参数定义
    config2.name=demo_1
    config2.value=demo_value_1</pre>

    然后,创建一个配置类Config2,用于将定义的变量映射到配置类里面。

    <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
    @ConfigurationProperties(prefix = "config2")
    public class Config2 {

    public String name;
    
    public String value;
    

    }</pre>

    读取数据的方式,也很简单,直接注入到对应的类里面就可以了

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RestController
    public class HelloController {

    @Autowired
    private Config2 config2;
    
    @GetMapping("config2")
    public String config2(){
        return JSON.toJSONString(config2);
    }
    

    }</pre>

    配置一个Map类型的参数

    例如在application.properties文件里,当我们想配置一个 Map 类型的参数,我们可以这样操作!

    <pre class="prettyprint hljs delphi" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//参数定义
    config3.map1.name=demo_id_1_name
    config3.map1.value=demo_id_1_value
    config3.map2.name=demo_id_2_name
    config3.map2.value=demo_id_2_value</pre>

    然后,创建一个配置类Config3,用于将定义的变量映射到配置类里面。

    <pre class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
    @ConfigurationProperties(prefix = "config3")
    public class Config3 {

    private Map<String, String> map1 = new HashMap<>();
    
    private Map<String, String> map2 = new HashMap<>();
    

    }</pre>

    读取数据的方式,与之类似!

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RestController
    public class HelloController {

    @Autowired
    private Config3 config3;
    
    @GetMapping("config3")
    public String config3(){
        return JSON.toJSONString(config3);
    }
    

    }</pre>

    配置一个List类型的参数

    例如在application.properties文件里,当我们想配置一个 List 类型的参数,我们可以这样操作!

    <pre class="prettyprint hljs delphi" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//参数定义
    config4.userList[0].enable=maillist_1_enable
    config4.userList[0].name=maillist_1_name
    config4.userList[0].value=maillist_1_value

    config4.userList[1].enable=maillist_2_enable
    config4.userList[1].name=maillist_2_name
    config4.userList[1].value=maillist_2_value

    config4.userList[2].enable=maillist_3_enable
    config4.userList[2].name=maillist_3_name
    config4.userList[2].value=maillist_3_value</pre>

    然后,创建一个配置类Config4,用于将定义的变量映射到配置类里面。

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
    @ConfigurationProperties(prefix = "config4")
    public class Config4 {

    private List<UserEntity> userList;
    
    public List<UserEntity> getUserList() {
        return userList;
    }
    
    public void setUserList(List<UserEntity> userList) {
        this.userList = userList;
    }
    

    }</pre>

    <pre class="prettyprint hljs vbscript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class UserEntity {

    private String enable;
    private String name;
    private String value;
    

    }</pre>

    读取数据的方式,与之类似!

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RestController
    public class HelloController {

    @Autowired
    private Config4 config4;
    
    @GetMapping("config4")
    public String config4(){
        return JSON.toJSONString(config4);
    }
    

    }</pre>

    2.3、通过@PropertySource注解实现配置文件加载

    正如我们最开始所介绍的,很多时间,我们希望将配置文件分卡写,比如zookeeper组件对应的服务配置文件是zookeeper.properties,redis组件对应的服务配置文件是redis.properties等等。

    这种自定义的配置文件,我们应该如何加载到Spring容器里面呢?

    其实方法也很简单,通过@PropertySource就可以实现!

    首先,我们在resources资源文件夹下,创建两个配置文件test.properties和bussiness.properties,内容如下!

    test.properties文件内容:

    <pre class="prettyprint hljs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">aaa.a1=aa1123
    aaa.a2=aa2123
    aaa.a3=aa3123
    aaa.a4=aa4123</pre>

    bussiness.properties文件内容:

    <pre class="prettyprint hljs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">bbbb.a1=bb1123
    bbbb.a2=bb2123
    bbbb.a3=bb3123
    bbbb.a4=bb4123</pre>

    在SpringBoot启动类上加载配置文件即可!

    <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@SpringBootApplication
    @PropertySource(value = {"test.properties","bussiness.properties"})
    public class PropertyApplication {

    public static void main(String[] args) {
        SpringApplication.run(PropertyApplication.class, args);
    }
    

    }</pre>

    读取数据的方式,与之类似!

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RestController
    public class HelloController {

    @Value("${aaa.a2}")
    private String a2;
    
    @Value("${bbbb.a1}")
    private String bbbbA1;
    
    @GetMapping("a2")
    public String a2(){
        return JSON.toJSONString(a2);
    }
    
    @GetMapping("bbbbA1")
    public String bbbbA1(){
        return JSON.toJSONString(bbbbA1);
    }
    

    }</pre>

    如果我们只是在业务中需要用到自定义配置文件的值,这样引入并没有什么问题;但是如果某些自定义的变量,在项目启动的时候需要用到,这种方式会存在一些问题,原因如下:

    image.png

    翻译过来的意思就是说:

    虽然在@SpringBootApplication上使用@PropertySource似乎是在环境中加载自定义资源的一种方便而简单的方法,但我们不推荐使用它,因为SpringBoot在刷新应用程序上下文之前就准备好了环境。使用@PropertySource定义的任何键都加载得太晚,无法对自动配置产生任何影响。

    因此,如果某些参数是启动项变量,建议将其定义在application.properties或application.yml文件里面,这样就不会有问题!

    或者,采用【自定义环境处理类】来实现配置文件的加载!

    2.4、通过自定义环境处理类,实现配置文件的加载

    实现方法也很简单,首先,创建一个实现自EnvironmentPostProcessor接口的类,然后自行加载配置文件。

    <pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    
        String[] profiles = {
                "test.properties",
                "bussiness.properties",
                "blog.yml"
        };
    
        for (String profile : profiles) {
    
            Resource resource = new ClassPathResource(profile);
    
            environment.getPropertySources().addLast(loadProfiles(resource));
        }
    }
    
    private PropertySource<?> loadProfiles(Resource resource) {
        if (!resource.exists()) {
            throw new IllegalArgumentException("资源" + resource + "不存在");
        }
        if(resource.getFilename().contains(".yml")){
            return loadYaml(resource);
        } else {
            return loadProperty(resource);
        }
    }
    
    private PropertySource loadProperty(Resource resource){
        try {
    
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            return new PropertiesPropertySource(resource.getFilename(), properties);
        }catch (Exception ex) {
            throw new IllegalStateException("加载配置文件失败" + resource, ex);
        }
    }
    
    private PropertySource loadYaml(Resource resource){
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource);
    
            Properties properties = factory.getObject();
            return new PropertiesPropertySource(resource.getFilename(), properties);
        }catch (Exception ex) {
            throw new IllegalStateException("加载配置文件失败" + resource, ex);
        }
    }
    

    }</pre>

    接着,在resources资源目录下,我们还需要创建一个文件META-INF/spring.factories,通过spi方式,将自定义环境处理类加载到Spring处理器里面,当项目启动时,会自动调用这个类!

    <pre class="prettyprint hljs dockerfile" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#启用我们的自定义环境处理类
    org.springframework.boot.env.EnvironmentPostProcessor=com.example.property.env.MyEnvironmentPostProcessor</pre>

    image.png

    这种自定义环境处理类方式,相对会更佳灵活,首先编写一个通用的配置文件解析类,支持properties和yml文件的读取,然后将其注入到Spring容器里面,基本上可以做到一劳永逸!

    2.5、最后,我们来介绍一下yml文件读取

    在上文中,我们大部分都是以properties为案例进行介绍,可能有的人已经踩过坑了,在项目中使用@PropertySource注解来加载yml文件,结果启动直接报错,原因是@PropertySource不支持直接解析yml文件,只能解析properties文件。

    那如果,我想单独解析yml文件,也不想弄一个【自定义环境处理类】这种方式来读取文件,应该如何处理呢?

    操作方式也很简单,以自定义的blog.yml文件为例!

    blog.yml文件内容:

    <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pzblog:
    name: helloWorld</pre>

    然后,创建一个读取yml文件的配置类

    <pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Configuration
    public class ConfigYaml {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("blog.yml"));
        configurer.setProperties(yaml.getObject());
        return configurer;
    }
    

    }</pre>

    读取数据的方式,与之类似!

    <pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RestController
    public class HelloController {

    @Value("${pzblog.name}")
    private String pzblogName;
    
    @GetMapping("pzblogName")
    public String pzblogName(){
        return JSON.toJSONString(pzblogName);
    }
    

    }</pre>

    三、小结

    本文主要围绕 SpringBoot 加载配置文件的几种实现方式,做了一次内容总结,如果有遗漏的地方,欢迎网友批评指出!

    四、参考

    1、springBoot 官方文档

    相关文章

      网友评论

          本文标题:教会你如何将自定义的配置文件注入到SpringBoot

          本文链接:https://www.haomeiwen.com/subject/nltnortx.html