tags:

views:

411

answers:

3

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.

A: 

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.

duffymo
The advantage is to not have the same string specified in 300 places in your dozen config files.
zvolkov
No advantage at all - I don't have anything more than once, and I don't have dozens of config files. You're not using Spring properly if you do.
duffymo
I have a solution with ~25 projects. Some of the projects implement libraries used in a few other projects.
zvolkov
+1  A: 

Use placeholders eg ${magic} and define the key/value in a properties file along with a PostProcessor. Google for spring post processor placeholder...

mP
+2  A: 

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.

flicken
+1 - PropertyPlaceholderConfigurer what I use, except for the system properties override. No need for that in my case.
duffymo