java - 如何使用@Value Spring Annotation 注入(inject) Map?

标签 java spring dependency-injection annotations spring-annotations

如何在 Spring 中使用 @Value 注释从属性文件中将值注入(inject) Map?

我的 Spring Java 类是,我尝试使用 $,但收到以下错误消息:

Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.test.standard' in string value "${com.test.standard}"

@ConfigurationProperty("com.hello.foo")
public class Test {

   @Value("${com.test.standard}")
   private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>

   private String enabled;

}

我在 .properties 文件中有以下属性

com.test.standard.name1=Pattern1
com.test.standard.name2=Pattern2
com.test.standard.name3=Pattern3
com.hello.foo.enabled=true

最佳答案

您可以像这样使用 @Value 注释从属性文件中将值注入(inject) Map。

属性文件中的属性。

propertyname={key1:'value1',key2:'value2',....}

在您的代码中。

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

请注意标签作为注释的一部分。

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

相关文章:

java - 将 Hibernate 5.2 与 Spring 框架 4.x 集成

java - 为什么 Autowiring 对于这个 Spring MVC String bean 属性不起作用?

jpa - Jersey + HK2 + 灰熊 : Proper way to inject EntityManager?

java - ListItem焦点改变

java - 当卡片布局中的面板出现时如何执行某些操作

java - 一个应用程序可以窃取其他应用程序的数据库连接吗?

c# - 使用 'where' 谓词从 Windsor CaSTLe 中的 Assembly 注册类型

java - 在 Eclipse 中播放视频工作正常,但是当将 java 项目导出为可运行的 JAR 文件时,它不再工作

java - 带有 ObjectID 的 Spring Data Mongo 自定义存储库查询

unit-testing - 文件访问、单元测试、依赖注入(inject)