使用@Value注解从配置文件中读取数组
@Value注解从配置文件读取数组
作用:从配置文件中取值
用法:
1.取单个值
(1)configuration.properties配置
status.notice.switch=open
(2)java文件自动注入
@Value("${status.notice.switch}")
private String statusNoticeSwitch;
2.取数组
(1)configuration.properties配置
lanwon.hospital.id=43534,234543,w353654
(2)java文件自动注入
@Value("#{'${lanwon.hospital.id}'.split(',')}")
private List<String> hospitalIdList;
使用@Value注解注入值(配置文件读取)
在 Spring 组件中使用 @Value 注解的方式,可以直接从 .properties,.yum 等配置文件获取配置信息便于实现项目的配置化运行。
1. 配置方式
1.1 使用
1、@Value("${key}")
2、@Value("#{configProperties[‘key']}") (SpEL表达式)
1.2 默认值配置
1、基础方式: ${key}:defaultvalue
2. SpEL方式:
使用 Spring Expression Language (SpEL) 设置默认值。
下面的代码标示在systemProperties属性文件中,如果没有设置 some.key 的值,my default system property value 会被设置成默认值。
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;
2. 使用场景
2.1 声明的变量
public static class FieldValueTestBean {
@Value("#{ systemProperties['user.region'] }")
private String defaultLocale;
}
2.2 setter 方法
public static class PropertyValueTestBean {
private String defaultLocale;
@Value("#{ systemProperties['user.region'] }")
public void setDefaultLocale(String defaultLocale) {
this.defaultLocale = defaultLocale;
}
}
2.3 方法
public class SimpleMovieLister {
private MovieFinder movieFinder;
private String defaultLocale;
@Autowired
public void configure(MovieFinder movieFinder,
@Value("#{ systemProperties['user.region'] }") String defaultLocale) {
this.movieFinder = movieFinder;
this.defaultLocale = defaultLocale;
}
// ...
}
2.4 构造方法
public class MovieRecommender {
private String defaultLocale;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
@Value("#{systemProperties['user.country']}") String defaultLocale) {
this.customerPreferenceDao = customerPreferenceDao;
this.defaultLocale = defaultLocale;
}
// ...
}
3 各种属性的注入及其默认值设置
3.1 字符串
字符串类型的属性设置默认值。
@Value("${some.key:my default value}")
private String stringWithDefaultValue;
some.key 没有设置值,stringWithDefaultValue 变量值将会被设置成 my default value 。
如果默认值设为空,也将会被设置成默认值。
@Value("${some.key:}")
private String stringWithBlankDefaultValue;
3.2 基本类型
基本类型设置默认值。
@Value("${some.key:true}")
private boolean booleanWithDefaultValue;
@Value("${some.key:42}")
private int intWithDefaultValue;
3.3 包装类型
包装类型设置默认值。
@Value("${some.key:true}")
private Boolean booleanWithDefaultValue;
@Value("${some.key:42}")
private Integer intWithDefaultValue;
3.4 数组
数组的默认值可以使用逗号分割。
@Value("${some.key:one,two,three}")
private String[] stringArrayWithDefaults;
@Value("${some.key:1,2,3}")
private int[] intArrayWithDefaults;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
利用Springboot+Caffeine实现本地缓存实例代码
Caffeine是一个基于Java8开发的提供了近乎最佳命中率的高性能的缓存库,下面这篇文章主要给大家介绍了关于利用Springboot+Caffeine实现本地缓存的相关资料,需要的朋友可以参考下2023-01-01
SpringBoot整合Logback日志框架及高并发下的性能优化
在现代的Java应用开发中,日志记录是不可或缺的一部分,Spring Boot作为目前最流行的Java开发框架之一,默认集成了Logback作为日志框架,Logback是Log4j的继任者,具有更高的性能和更丰富的功能,本文将详细介绍如何在Spring Boot中整合Logback日志框架2025-03-03
Springboot集成Mybatis-plus、ClickHouse实现增加数据、查询数据功能
本文给大家讲解Springboot + mybatis-plus 集成ClickHouse,实现增加数据、查询数据功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧2024-08-08


最新评论