Embed an external property in a Spring context

I have three applications in a Spring 2.5 managed project that has common code and different details.

Each application has a property ( java.lang.String), which is used before creating the application context.

Building the application context takes some time and may not be the first. Thus, it is defined in each individual application. This property is duplicated in the context definition, since it is also needed there. Can I get rid of this duplication?

Can I insert this property into my application context?

+5
source share
3 answers

Take a look PropertyPlaceholderConfigurer.

Spring .

<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:my-property-file.properties"/>
    <property name="placeholderPrefix" value="$myPrefix{"/>
</bean>

<bean id="myClassWhichUsesTheProperties" class="com.class.Name">
    <property name="propertyName" value="$myPrefix{my.property.from.the.file}"/>
</bean>

, , constructor-arg, property ..

+5

spring 3.0 @Value("${property}"). PropertyPlaceholderConfigurer beans.

spring 2.5 PropertyPlaceholderConfigurer, bean java.lang.String, autwire:

<bean id="yourProperty" class="java.lang.String">
    <constructor-arg value="${property}" />
</bean>

@Autowired
@Qualifier("yourProperty")
private String property;
+3

, bean

<bean id="parent" class="my.class.Name"/>

- spring xml, common.xml. - xml :

<import resource="common.xml"/>

and then you can enter the properties of your parent in the beans that interests you:

<bean ...
<property name="myProperty" value="#{parent.commonProperty}"/>
...
</bean>
+1
source

All Articles