Can I set the system property for constructor-arg in the spring configuration file?

I have a spring configuration file that includes the following elements:

<context:property-placeholder location="classpath:default.properties"/>

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

"varName" is now moved from the properties file to the system property. It is added when I run the Maven build:

mvn clean install -DvarName=data

I want to also run my build without specifying varName:

mvn clean install

Is there a default way varName in my spring configuration? Although this does not work, a conceptual example of what I'm looking for is:

<bean id="theVar" class="java.lang.String">
    <constructor-arg value="${varName}" default="theDefaultValue"/>
</bean>
+5
source share
4 answers

, spring v2.5 +, , , . , "" .

, :

<!-- my spring config file -->
<context:property-placeholder location="classpath:default.properties" system-properties-mode="OVERRIDE"/>

# default.properties file
theVariable=One

:

mvn clean install

"" . :

mvn clean install -DtheVariable=Two

"".

+4

Spring 3.0.x :

value="${varName:defaultValue}"

:

+7

. , , , , :

@Value("#{systemProperties['fee.cc']?:'0.0225'}")
public void setCcFeePercentage(BigDecimal ccFeePercentage) {
    this.setCcFeePercentage(ccFeePercentage);
}
+3

This can be done as described in @sebastien, but in the configuration file, as you want:

<bean id="theVar" class="java.lang.String">
    <constructor-arg value="#{systemProperties['varName'] == null ? 'default_value' : systemProperties['varName']}"/>
</bean>

If your variable is varNamemissing, the default value will be set.

+1
source

All Articles