springboot读取yml配置文件里的属性到Java代码,一般有两种常用方式:
- 使用注解@ConfigurationProperties批量读取
- 使用注解@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.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</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
解决:
(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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/80324.html