40077

Question:
In my Spring MVC application, I want to read <strong>ALL key/values</strong> 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 <strong>map</strong>
Thanks Manu
Answer1:One way to achieve the same is <a href="https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object" rel="nofollow">Spring: access all Environment properties as a Map or Properties object</a> 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;