Application Configuration (Spring?)

java, spring

Solution

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

Problem

I'm getting tired of all this boring boilerplate code to parse application configuration like database connections, working directories, API endpoints and whatnot. Spring 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?

Original source