@Value使用Spring注入地图地图?



我们有一个Java应用程序,它使用Spring(5.0.8(进行依赖注入,但不由Spring Boot管理。简单的@Value注入是通过在我们的 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.....

应用程序上下文由

AnnotationConfigApplicationContext APPLICATION_CONTEXT =
new AnnotationConfigApplicationContext(MyConfig.class)

然后我可以开始使用属性作为

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

现在我想使用 SpEL 添加@Value注入,例如

@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.addLast( new ResourcePropertySource( "classpath:myMap.properties"));

我收到了这样的错误

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

因为不认识我的@Value注射

谁能对此有所了解?

我发现了问题。憨。行尾需要反斜杠

最新更新