【发布时间】:2019-03-29 06:40:15
【问题描述】:
我们有一个使用 Spring (5.0.8) 进行依赖注入但不受 Spring Boot 管理的 java 应用程序。简单的@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.....
ApplicationContext 由
启动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 中,例如
propertySources.addLast( new ResourcePropertySource( "classpath:myMap.properties"));
我得到了类似
的错误Expression [#{{}] @0: No ending suffix '}' for expression starting at character 0: #{{}
因为不认识我的@Value 注射
任何人都可以对此有所了解吗?
【问题讨论】:
-
我正在尝试关注stackoverflow.com/questions/47272477/…,但它根本不适合我。
-
你试过
@Value("${usermap}")吗? -
TBH @Value 不鼓励,您应该使用类型安全的配置属性。 docs.spring.io/spring-boot/docs/current/reference/html/…
标签: java spring spring-boot dependency-injection properties