We are using a PropertyPlaceholderConfigurer to use java properties in our Spring configuration (details here)
eg:
<foo name="port"> <value>${my.server.port}</value> </foo>
We would like to add an additional property, but have a distributed system where existing instances could all use a default value. Is there a way to avoid updating all of our properties files, by indicating a default value in the Spring config for when there isn't an overriding property value defined?
To set a default value for primitive types such as boolean and int, we use the literal value: @Value("${some. key:true}") private boolean booleanWithDefaultValue; @Value("${some.
The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created. For example, if you set the DefaultValue property for a text box control to =Now(), the control displays the current date and time.
Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.
Spring 3 supports ${my.server.port:defaultValue}
syntax.
There is a little known feature, which makes this even better. You can use a configurable default value instead of a hard-coded one, here is an example:
config.properties:
timeout.default=30 timeout.myBean=60
context.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>config.properties</value> </property> </bean> <bean id="myBean" class="Test"> <property name="timeout" value="${timeout.myBean:${timeout.default}}" /> </bean>
To use the default while still being able to easily override later, do this in config.properties:
timeout.myBean = ${timeout.default}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With