gpt4 book ai didi

java - @Value使用Spring注入(inject) map 的 map ?

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:33 24 4
gpt4 key购买 nike

我们有一个 java 应用程序,它使用 Spring (5.0.8) 进行依赖注入(inject),但不受 Spring Boot 管理。简单的 @Value 注入(inject)是通过在我们的 Java 配置类中实现的

final PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
StandardEnvironment myEnv = new StandardEnvironment();
try {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst( new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
myEnv.getSystemProperties()));
propertySourcesPlaceholderConfigurer.setPropertySources(propertySources);
}catch.....

ApplicationContext 是由

启动的
AnnotationConfigApplicationContext APPLICATION_CONTEXT =
new AnnotationConfigApplicationContext(MyConfig.class)

然后我就可以开始使用这些属性了

@Component
public class MyService {
@Value( "${"myvalInSystemProperty"}")
private String myval;

........
}

现在我想使用 SpEL 添加一个 @Value 注入(inject)

@Component
public class MyService {

@Value("#{${usermap}}")
private Map<String, Map<String, String>> usermap;

........
}

对应的属性文件是

usermap = {
key1:{
subkey1:'subvalue1',
subkey2:'subvalue2'
},
key2:{
subkey3:'subvalue3',
subkey4:'subvalue4'
}
}

我已将属性文件添加到 propertySources 中,例如

propertySources.addLast( new ResourcePropertySource( "classpath:myMap.properties"));

我遇到了类似的错误

Expression [#{{}] @0: No ending suffix '}' for expression starting at character 0: #{{}

无法识别我的@Value注入(inject)

有人可以解释一下吗?

最佳答案

我发现问题了。愚蠢的。行尾需要反斜杠

关于java - @Value使用Spring注入(inject) map 的 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52975923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com