【问题标题】:@Value injection a map of maps using Spring?@Value 使用 Spring 注入地图地图?
【发布时间】: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 注射

任何人都可以对此有所了解吗?

【问题讨论】:

标签: java spring spring-boot dependency-injection properties


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 2013-04-23
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2017-04-24
    相关资源
    最近更新 更多