<util:properties> equivalent in java based configuration for spring

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.

Tags:

Java

Spring