java - 在 Spring Boot 中使用 @Value 从应用程序 YML 中的 List 创建 Map
问题描述
我正在尝试在Map<Integer,List<Integer>>
Java 服务中创建一个变量。
我有这个.yml:
my:
data:
'{
1:[1,2,3],
2:[1,2,3]
}'
并进入Java代码:
@Value("#{${my.data}}")
protected Map<Integer,List<Integer>> bar;
但是当我运行项目时它失败了。
实际上抛出的错误是这样的:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller' defined in file ...
但它必须是通过依赖注入,它在创建时失败@Value
并且@Service
错误被传播。我也测试了这些值
my:
data:
'{
1:
- 1
- 2,
2:
- 1
}'
它会创建一个包含值-3
和的列表-1
。
| key | value |
+-----+-------+
| 1 | [-3] |
| 2 | [-1] |
+-----+-------+
所以之前抛出的错误肯定是由于 first 中列表的定义造成的yml
。
我也测试了使用List<Integer>
和int[]
进入Map
对象。
那么,创建的正确语法是Map<Integer, List<Integer>>
什么?我认为它就像一个 JSON 对象{ key: [v1, v2] }
,但它似乎失败了。
提前致谢。
解决方案
带@Value
注释:
您不应该将(yaml 或属性“map”)键映射到
Integer
,更喜欢String
.(问题)嵌套列表...
..不过,我很确定 (crazy SpEL(l) @Value
) 是可能的。
但是类型安全的配置属性会很快产生很好的结果:
应用程序.java:
@SpringBootApplication
@ConfigurationPropertiesScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
CommandLineRunner runner(@Autowired My bar) {
return args -> System.out.println(bar);
}
}
我的.java:
@ConfigurationProperties("my")
public class My {
private Map<Integer, List<Integer>> data = new HashMap<>();
// Getter + Setter!!!
}
有了这个“固定”的 yaml:
my:
data: {
1: [1,2,3],
2: [1,2,3]
}
印刷:
My(data={1=[1, 2, 3], 2=[1, 2, 3]})
编辑:
@Value
到目前为止,我得到的“最好的”是:
@Value("#{${other.data}}") Map<?, ?> other; // raw map!! :-)
使用这个 yaml(类似):
other:
data: '{
1: ''[1,2,3]'',
2: ''[1,2,3]''
}'
印刷:
{1=[1,2,3], 2=[1,2,3]}
(jdk:8,maven:3.8.2,spring-boot:2.5.6)
推荐阅读
- javascript - Highcharts 实心仪表未完全着色
- rest - 如何为特定情况设计rest uri?
- wordpress - Woocommerce 钩子 - 从产品类别页面中删除类别标题
- ruby - 如何以编程方式将值数组添加到键?
- android - 使用带有 AWS Android 开发工具包的 Lambda 触发器将用户迁移到
- reactjs - 遍历子元素,找到最后一个元素
- amazon-cloudwatch - 带有winston的aws cloudwatch控制台中出现的不是颜色,而是符号
- google-cloud-platform - 访问/管理地图自动完成等 API 服务需要哪些 GCP IAM 权限?
- selenium - 发送每个键的速度较慢,因为文本框无法快速处理发送键
- c# - HangFire 在给定时间 3 次重复作业