Malax
Malax

Reputation: 9604

Application Configuration (Spring?)

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?

Upvotes: 5

Views: 6883

Answers (6)

Brian Agnew
Brian Agnew

Reputation: 272217

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.

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.

Upvotes: 2

Nick Holt
Nick Holt

Reputation: 34301

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

Upvotes: 0

cletus
cletus

Reputation: 625007

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

Upvotes: 10

Nate
Nate

Reputation: 16888

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.

Upvotes: 2

cafebabe
cafebabe

Reputation: 1400

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.

Upvotes: 0

user7094
user7094

Reputation:

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!

Upvotes: 5

Related Questions