spring-boot-route 读取配置文件的几种方式

摘要:
SpringBoot提供两种格式的配置文件:properties和yml。配置文件集中管理配置信息。如果将配置参数写入Java代码,则维护非常不方便。如果使用配置文件,我们可以统一管理和修改它。

Spring Boot提供了两种格式的配置文件,分别是properties 和 yml。Spring Boot最大的特点就是自动化配置,如果我们想修改自动化配置的默认值,就可以通过配置文件来指定自己服务器相关的参数。

配置文件集约管理了配置信息,如果把配置参数写到Java代码中,维护起来非常不方便,如果使用配置文件,我们可以统一管理,统一修改。我比较推荐使用yml格式的配置文件,YAML是专门用来写配置文件的语言,通常以yml为后缀,它的结构非常清晰,更易于阅读。

将自定义的配置写在配置文件中后,如果想要在java代码中使用配置,这时候就需要读取配置文件,读取配置文件的方式有三种,我们挨个介绍一下如果进行读取!

第一种:使用@Value注解读取

第一步:在配置文件中增加加入以下配置

config:
  name: Java旅途
  desc: spring-boot-route

第二部:新建Java类读取配置信息

@RestController
public class GetValue {

    @Value("${config.name}")
    private String name;

    @Value("${config.desc}")
    private String desc;

    @GetMapping("getValue")
    public String getValue(){
        return "name="+name+";desc="+desc;
    }
}

@Value注解使用简单,适合单个参数的注入。

第二种:使用@ConfigurationProperties读取

@ConfigurationProperties与@Value相比,更加适合读取数组类型的参数。

1. 获取单个对象

第一步:在yml文件中新建对象类型的配置信息

configs:
  config:
    name: Java旅途
    desc: spring-boot-route

第二步:新建实体映射配置信息

@Component
@ConfigurationProperties(prefix = "configs.config")
@Data
public class Config {

    private String name;
    private String desc;
}

第三步:新建类测试是否获取到参数

@RestController
public class GetConfigurationProperties {

    @Autowired
    private Config config;

    @GetMapping("/getConfig")
    public String getConfig(){
        return config.getName()+";"+config.getDesc();
    }
}

2. 获取对象集合

第一步:在yml文件中新建数组类型的参数

configs:
  config:
    - name: Java旅途
      desc: spring-boot-route
    - name: javatrip
      desc: spring-boot-yml

第二步:新建实体映射配置信息

@Component
@ConfigurationProperties(prefix = "configarr")
@Data
public class Configs {

    private List<Config> config = new ArrayList<>();

    @Data
    public static class Config{

        private String name;
        private String desc;
    }
}

第三步:新建测试类获取参数

@RestController
public class GetConfigurationProperties {

    @Autowired
    private Configs configs;
    
    @GetMapping("/getConfigs")
    public String getConfigs(){

        String content = "";
        List<Configs.Config> configList = configs.getConfig();
        Map<String,Object> map = new HashMap<>();
        for (Configs.Config bean : configList){
            content += bean.getName()+";"+bean.getDesc()+",";
        }
        return content;
    }
}

除了上面介绍的两种方式之外,还可以通过Spring Boot上下文的环境变量来读取配置文件信息

免责声明:文章转载自《spring-boot-route 读取配置文件的几种方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Google的PageRank算法浅析服务器双向同步( 可实时 ) unison + inotify下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

netty 实现socket服务端编写

1 importjava.net.InetSocketAddress; 2 3 importio.netty.bootstrap.ServerBootstrap; 4 importio.netty.channel.ChannelFuture; 5 importio.netty.channel.ChannelInitializer; 6 impo...

金蝶云星空使用WebAPI来新增单据

有很多客户需求在后台自动生成某张单据,金蝶云星空提供了WebApi,包含了保存,提交,审核,删除单据的接口,下面以生产订单的保存,提交,审核为例,说明一下应用WebApi后台自动生成生产订单的功能,下面是代码示例,其他单据可以根据示例代码做相应的修改: using Kingdee.BOS.Core.Bill.PlugIn;using System;usin...

Spire.XLS 在程序中直接打印excel

上代码 if (tbPrintSetBindingSource.Current == null) return; var item_TbPrintSet = tbPrintSetBindingSource.Current as RMES.IBatisEntity.TbPrintSet; fi...

Delphi XE4 TStringHelper用法详解

原文地址:DelphiXE4TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到。System.SysUtils.TStringHelper大小写转换:-------------------------------------------------...

Java接口自动化测试之HTTPClient学习(四)

pom.xml  文件中dependency <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId>...

[OAuth]基于DotNetOpenAuth实现Client Credentials Grant

Client Credentials Grant是指直接由Client向Authorization Server请求access token,无需用户(Resource Owner)的授权。比如我们提供OpenAPI让大家可以获取园子首页最新随笔,只需验证一下Client是否有权限调用该API,不需要用户的授权。而如果Client需要进行发布博客的操作,就...