Spring MVC @PropertySource将所有Key / Value作为地图 [英] Spring MVC @PropertySource all Key/value as a map

查看:469
本文介绍了Spring MVC @PropertySource将所有Key / Value作为地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring MVC应用程序中,我想从指定的属性文件中读取所有键/值
我将属性文件包含到我的java类中

  @PropertySource(classpath:user-form-validation -configuration.properties)

,一次可以读取一个键

  @Autowired 
环境env;

env.getProperty(userIdEmail)



请帮助我如何获取所有键/值作为地图



感谢
Manu

解决方案

实现相同的一种方法是 Spring:作为Map或Properties对象访问所有Environment属性,其次是: / p>

 < bean id =myPropertiesclass =org.springframework.beans.factory.config.PropertiesFactoryBean> 
< property name =locationvalue =classpath:user-form-validation-configuration.properties/>
< / bean>

对于基于注释:

  @Bean(name =myProperties)
public static PropertiesFactoryBean mapper(){
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource(
user-form-validation-configuration.properties));
返回bean;
}

然后,您可以在应用程序中选择:

  @Resource(name =myProperties)
private Map< String,String> myProperties;


In my Spring MVC application, I want to read ALL key/values from a specify properties file. I am including the properties file to my java class by

@PropertySource("classpath:user-form-validation-configuration.properties")

and can read a one key at a time

@Autowired
Environment env;

and env.getProperty("userIdEmail")

Please help me how to get all key/value as a map

Thanks Manu

解决方案

One way to achieve the same is Spring: access all Environment properties as a Map or Properties object and secondly is:

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:user-form-validation-configuration.properties"/>
</bean>

For, Annotation based:

@Bean(name = "myProperties")
public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource(
                "user-form-validation-configuration.properties"));
        return bean;
}

Then you can pick them up in your application with:

@Resource(name = "myProperties")
private Map<String, String> myProperties;

这篇关于Spring MVC @PropertySource将所有Key / Value作为地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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