springboot读取yml配置文件

springboot读取yml配置文件里的属性到Java代码,一般有两种常用方式:

  1. 使用注解@ConfigurationProperties批量读取
  2. 使用注解@Value单个读取

示例yml文件:

data:
  elasticsearch:
    cluster-name: elasticsearch
    cluster-nodes: 192.168.1.135:9200
    scheme: http
    maxRetry_Timeout_Millis: 60000
    socketTimeout: 10000
    scrollSize: 3000
    user: elastic
    password: soft

一、 使用@ConfigurationProperties批量读取

如果我们要读取data.elasticsearch下面的所有配置可以使用@ConfigurationProperties批量读取。

使用@ConfigurationProperties需要的依赖

<dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-configuration-processorartifactId>
        <optional>trueoptional>
 dependency>

读取yml类:

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "data.elasticsearch")
public class ConfigurationFileModel {
    String clustername;
    String clusternodes;
    String scheme;
    String maxRetryTimeoutMillis;
    String socketTimeout;
    String scrollSize;
    String user;
    String password;

   //省略get、set方法
  //.......
}

然后会发现注解@ConfigurationProperties下报错Not registered via@EnableConfigurationProperties or marked as Spring component
springboot读取yml配置文件_第1张图片
解决:
(1)方法一:在读取yml类上加上

@Component

(2)方法二:在使用到读取类的类上加上:

@Configuration
@EnableConfigurationProperties(ConfigurationFileModel.class)

测试:

@Configuration
@EnableConfigurationProperties(ConfigurationFileModel.class)
@Service
public class UserConsumer {
  
    @Autowired
    ConfigurationFileModel configurationFileModel;
    
    public void configurationFile() {
        System.out.println("clustername:" + configurationFileModel.getClustername());
        System.out.println("clusternodes:" + configurationFileModel.getClusternodes());
        System.out.println("scheme:" + configurationFileModel.getScheme());
        System.out.println("maxRetryTimeoutMillis:" + configurationFileModel.getMaxRetryTimeoutMillis());
        System.out.println("socketTimeout:" + configurationFileModel.getSocketTimeout());
        System.out.println("scrollSize:" + configurationFileModel.getScrollSize());
        System.out.println("user:" + configurationFileModel.getUser());
        System.out.println("password:" + configurationFileModel.getPassword());
    }
}

输出:

clustername:elasticsearch
clusternodes:192.168.1.135:9200
scheme:http
maxRetryTimeoutMillis:60000
socketTimeout:10000
scrollSize:3000
user:elastic
password:soft

二、 使用@Value单个读取属性

使用@Value读取比较简单,用到的时候直接读取就行。

无默认值的写法:
data.elasticsearch下如果不存在user、password就会报错

    @Value("${data.elasticsearch.user}")
    String user;
    @Value("${data.elasticsearch.password}")
    String password;

有默认值的写法:
data.elasticsearch下如果不存在user、password不会报错,会使用默认值admin和123456

    @Value("${data.elasticsearch.user:admin}")
    String user;
    @Value("${data.elasticsearch.password:123456}")
    String password;

测试:

@Service
public class UserConsumer {
  
    @Value("${data.elasticsearch.user:admin}")
    String user;
    @Value("${data.elasticsearch.password:123456}")
    String password;
    
    public void configurationFile() {
        System.out.println("user:" + user);
        System.out.println("password:" + password);
    }
}

输出:

user:elastic
password:soft

你可能感兴趣的:(java,spring,boot,spring,java)