Issue
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;
Looking at the doc, I looked at the
@PropertySource
annotation but it seems to me that the particular propertyfile will not be able to be accessed individually from the Environment object.
Solution
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;
}
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>
Converting that to Java config is super easy as shown above.
Answered By - Sotirios Delimanolis
Answer Checked By - Willingham (JavaFixing Volunteer)