Spring Util:通过注释将属性注入到bean中 [英] Spring Util:Properties Injection via Annotations into a bean

查看:142
本文介绍了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.

这篇关于Spring Util:通过注释将属性注入到bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆