< UTIL:性状>相当于基于java的spring配置 [英] <util:properties> equivalent in java based configuration for spring

查看:138
本文介绍了< UTIL:性状>相当于基于java的spring配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于XML的弹簧配置基于java配置的等价物

What would be the equivalent in java based configuration of XML based spring configuration

<util:properties id="mapper"  location="classpath:mapper.properties" />

然后能够在以下代码中使用此特定属性对象:

To then be able to use this specific property object in code like :

@Resource(name = "mapper")
private Properties myTranslator;

查看文档,我查看了

@PropertySource

注释但在我看来无法从Environment对象单独访问特定的属性文件。

annotation but it seems to me that the particular propertyfile will not be able to be accessed individually from the Environment object.

推荐答案

很简单,声明 PropertiesFactoryBean

Very simply, declare a PropertiesFactoryBean.

@Bean(name = "mapper")
public PropertiesFactoryBean mapper() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("com/foo/jdbc-production.properties"));
    return bean;
}

在文档中这里,你'请注意,在他们制作< util:properties> 之前,他们过去常常使用 PropertiesFactoryBean

In the documentation here, you'll notice that before they made <util:properties>, they used to use a PropertiesFactoryBean as such

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean>

将其转换为Java配置非常简单,如上所示。

Converting that to Java config is super easy as shown above.

这篇关于&LT; UTIL:性状&gt;相当于基于java的spring配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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