Manu
Manu

Reputation: 1263

Spring MVC @PropertySource all Key/value as a map

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

Upvotes: 2

Views: 5951

Answers (1)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29276

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;

Upvotes: 4

Related Questions