in Spring.config, can I define object of type string?
-
03-07-2019 - |
Question
Can I do something like this:
<object id="mydb" type="string">
<value>"blah"</value> <-- note that <value> tag does not really exist
</object>
So that I could use it later like so:
<object id="Someobject" type="Sometype">
<property name="ConnectionString" ref="mydb"/>
</object>
EDIT: this was SpringFramework.NET solution I was looking for. Looks like PropertyPlaceholderConfigurer exists there too. Thanks everybody.
Solution
Use Spring's built-in PropertyPlaceholdConfigurer:
<bean id="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="classpath*:/application.properties"/>
</bean>
<bean id="Someobject" class="somepackage.Sometype">
<property name="connectionString" value="${mydb}"/>
</bean>
Setting SYSTEM_PROPERTIES_MODE_OVERRIDE
allows overriding the property via the command line.
OTHER TIPS
Use placeholders eg ${magic} and define the key/value in a properties file along with a PostProcessor. Google for spring post processor placeholder...
I don't see any advantage to your way at all. It's all still just configuration.
Sometimes people externalize database connection strings to a .properties file and get at them that way. I think that makes more sense than your proposal.