Fork me on GitHub

Overview

With the Settings4jPlaceholderConfigurer for the Springframework you can use Settings4j to inject Values into your beans.

Examples

In the following Example the Expression "${com/mycompany/myEntry}" will be replaced by the value form Settings4j.getString("com/mycompany/myEntry").

<beans>
  <!-- The PlaceholderConfigurer: -->
  <bean class="org.settings4j.helper.spring.Settings4jPlaceholderConfigurer" />
  
  <!-- Example usage of Placholder-Expressions: -->
  <bean id="myDummyMap" class="java.util.HashMap">
    <constructor-arg>
      <map>
        <entry key="MapEntry1"><value>${com/mycompany/myEntry}</value></entry>
      </map>
    </constructor-arg>
  </bean>
</beans>

Or inject the Value with the Spring Annotation (Spring 3.0+; http://static.springsource.org/spring/docs/3.0.x/reference/expressions.html#expressions-beandef-annotation-based):

@Value("${com/mycompany/myEntry}")
private String myEntry;

Example Prefixes

In most cases you will have the same Prefix for all Entries. So it's easier to define the Prefix in Settings4jPlaceholderConfigurer:

<bean class="org.settings4j.helper.spring.Settings4jPlaceholderConfigurer" >
  <property name="prefix" value="com/mycompany/"/>
</bean>

<bean id="myDummyMap" class="java.util.HashMap">
  <constructor-arg>
    <map>
      <entry key="MapEntry1"><value>${myEntry1}</value></entry>
      <entry key="MapEntry2"><value>${myEntry2}</value></entry>
    </map>
  </constructor-arg>
</bean>

So the values Settings4j.getString("com/mycompany/myEntry1") and Settings4j.getString("com/mycompany/myEntry2") will be read.

Example Default Values

You can also define Default Properties if nothing is defined for some optional KEYs:

<bean class="org.settings4j.helper.spring.Settings4jPlaceholderConfigurer" >
  <property name="prefix" value="com/mycompany/"/>
  <property name="properties">
    <props>
      <!-- Default Values -->
      <prop key="myEntry2">My Default Value for Entry 2</prop>
    </props>
  </property>
</bean>

Custom Converters

If you use the Spring-Placeholder for Settings4j, you must use a depends-on attribute to be sure that the initialization runs first:

<beans>
  <bean class="org.settings4j.helper.spring.Settings4jPlaceholderConfigurer" depends-on="initSettings4j"/>
  
  <!-- InitSettings4j.initSettings4j() will add the custom converter to the Settings4j instance. -->
  <bean id="initSettings4j" class="com.myProject.InitSettings4j" init-method="initSettings4j"/>
</beans>