如果我在 Spring XML 中设置了 2 个 .properties 文件:
<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/>
<util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/>
如何通过注解将这些属性文件注入(inject)到带有 java.util.Properties
的 bean 中?
如何通过 Spring 注释获取特定属性?
干杯!
最佳答案
@Autowired
@Qualifier("serverProperties")
private Properties serverProperties;
@Autowired
@Qualifier("someConfig")
private Properties otherProperties;
或
@Resource(name = "serverProperties")
private Properties serverProperties;
@Resource(name = "someConfig")
private Properties otherProperties;
通常,@Autowired 用于 Spring 中的按类型 Autowiring ,@Resource 用于按名称。 @Autowired+@Qualifier 可以兼作按名称 Autowiring ,但它实际上适用于具有 fine-tune the type 能力的按类型 Autowiring 。 .
关于java - Spring Util :Properties Injection via Annotations into a bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7219097/