Spring Util:通过注释将属性注入到bean中
问题描述:
如果我在我的Spring XML中设置了2个.properties文件:
If I have 2 .properties files setup in my Spring XML as so:
<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/>
<util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/>
如何通过注释将这些属性文件注入到具有 java的bean中。 util.Properties
?
How can I inject via annotations these properties files into a bean with java.util.Properties
?
如何通过Spring注释获取特定的属性?
How can I grab specific properties via Spring annotations?
干杯!
答
@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中的按类型自动装配,而@Resource用于-名称。 @ Autowired + @限定符可以按名称自动布线方式加倍,但它实际上用于通过类型自动布线来实现微调类型。
Typically, @Autowired is used for by-type autowiring in Spring, and @Resource is used for by-name. @Autowired+@Qualifier can double as by-name autowiring, but it's really meant for by-type autowiring with the ability to fine-tune the type.