tags:

views:

589

answers:

6

Hi Stackoverflow!

im getting tierd of all this boring boilerplate code to parse application configuration like database connections, working directories, API endpoints and whatnot. Sping IoC looks nice, but this will force the user of my application to modify the XML file just to edit the database URL and so on. This might also be very distributed in the XML file where all my other wiring ocours.

What is the best technique to allow end-users to configurate services (Which do not run inside an application server)? What go you guys use?

I'm looking forward for your answers, have a nice day!

+2  A: 

Don't forget you can use the Spring XML files to build your application, but use properties files to provide (say) database URLs etc. within these configurations. See here for more info.

This works very nicely when you have one set of XML files to build your application, and then provide a different set of properties files depending on whether you're running a development, test or production version of the application.

Brian Agnew
+5  A: 

Hi, you can store application configuration in a properties file and use PropertyPlaceholderConfigurer to load in those properties.

So, in your applicationContext file, you might have this:

<bean id="dataSource" class="com.x.y.z.DataSource">
    <property name="url" value="${dataSource.url}" />
</bean>

The dataSource.url property will get loaded in from your properties file.

This is what I use in an application I'm working on, and it makes configuration much easier!

Phill Sacre
A: 

You can modularize the Spring configuration files. So, you can have one XML file for the database connection, which is then included (imported) into a central Spring configuration file. If the user should be able to modify stuff, you can create the XML files from a template and (re)load the application context.

oeogijjowefi
+2  A: 

You can still use Spring to wire together your classes with IOC (in XML, Java config, autowiring, whatever) but still use property files to contain values that may change based on deployment like database connections.

Look at PropertyPlaceholderConfigurer. Here's a blog post with an example of how you'd use it.

Nate
+8  A: 

Use Spring, being explicit wiring in XML, auto-wiring or some combination thereof, to define "constant" config and then externalize the rest in properties files. Database credentials are a common example of this.

See Spring and Ibatis Tutorial for a baseline example of this. Short version:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:database.properties"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${database.class}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

with database.properties (in the classpath):

database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:@localhost:1521:XE
cletus
A: 

Use a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer explained here:

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

Nick Holt