springBoot如何优雅的读取配置文件

springBoot中有分别提供了两个注解来读取配置文件,分别是@Value(“${}”)与@ConfigurationProperties(prefix = ""),下面分别讲述两种在不同情况下的用法。

yml文件格式:

service:
  elasticsearch:
    url: llllllllllllllll
    username: 222222222222
    password: 4444444444444
  kafka:
    bootstrap-servers: 444444444444
    consumer:
      auto-offset-reset: latest                          
      max-poll-records: 3100                      
      enable-auto-commit: false                         
      auto-commit-interval: 1000                  
      session-timeout: 20000                              
    listener:
      batch-listener: true                               
      concurrencys: 5,10                                 
      poll-timeout: 1500  

第一种方式使用 @ConfigurationProperties(prefix = "")来进行读取:

package com.example.demo.example.text;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @title: readConfigs
 * @Author linzm
 * @Date: 2022/8/17 13:23
 * @Version 1.0
 */
@Component(value = "readConfigs")
@ConfigurationProperties(prefix = "service")
@Data
public class readConfigs {
    @Autowired
    Elasticsearch elasticsearch;
    @Autowired
    Kafka kafka;
    /**
     * 使用内部类来解决yml多层结构
     */
    @Data
    @Component
    @ConfigurationProperties(prefix = "service.elasticsearch")
    public static class Elasticsearch{
        private String url;
        private String username;
        private String password;

    }
    @Data
    @Component
    @ConfigurationProperties(prefix = "service.kafka")
    public static class Kafka{
        @Autowired
        Consumer consumer;

        @Autowired
        Listener listener;
        private String bootstrapServers;

    }


    @Data
    @Component
    @ConfigurationProperties(prefix = "service.kafka.consumer")
    public static class Consumer{
        private String autoOffsetReset;
        private String maxPollRecords;
        private String enableAutoCommit;
        private String autoCommitInterval;
        private String  sessionTimeout;

    }

    @Data
    @Component
    @ConfigurationProperties(prefix = "service.kafka.listener")
    public static class Listener{
        private String batchListener;
        private String concurrencys;
        private String pollTimeout;
    }


}

 测试:

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
      readConfigs readConfigs = (readConfigs) run.getBean("readConfigs");
      String autoCommitInterval = readConfigs.getKafka().getConsumer().getAutoCommitInterval();
      System.out.println(autoCommitInterval);
      System.out.println("启动成功.....");
   }

}

 以上就很成功的拿到了值。这样是不是看起来高大上了一点。

 

 第一种方式使用 @Value(“${}”)来进行读取:

package com.example.demo.example.text;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @title: ReadConfiga
 * @Author linzm
 * @Date: 2022/8/17 14:01
 * @Version 1.0
 */
@Component(value = "readConfiga")
@Data
public class ReadConfiga {
    @Value("${service.elasticsearch.url:100}")
    private String elasticsearch;

}

@Value(“${}”)要注意的是最好加个默认值,以防报错(:默认值);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值