My initial reaction was:
"Well, I could have the values/config stored in the database and then just cache them."
But that solution would require days because I would have to create the tables, get them ok'd by the db ops team, have them put in the different environments( test, integration, stage, and then finally production ). Then on top of that I would have to create some sort of tool to manage the values in the table so that they could be updated as needed. Then I would have to secure the the admin page for the config management. And so on and so forth.
Then it occurred to a co-worker and I that we could just externalize the Spring Bean definition and cache it for a set amount of time ( and of course relying on the last good configuration if the new one fails ). The external config for the Spring Bean could then live anywhere ( local file system, or over http on a server somewhere ).
This of course was by far the most flexible and convenient way of dealing with the situation. So here is how it works.
Step 1: Create Factory.
Step 2: Enter the JNDI config for location of your externalized resource in the applicationContext.xml:
<jee:jndi-lookup id="serviceFactoryConfig" startup="true"
Step 3: Wire factory up in applicationContext.xml
Set up the ServiceFactoryFactoryContextLoader with the cache proxy of course so that we can cache the retrieval and creation of the object. The property 'location' gets injected with the JNDI config value for the location of the externalized resource. And the cache:proxy allows me t cache any method beginning the 'get'. I am using ehcache for my caching but you can use others if you want/need to.
<property name="location" ref="serviceFactoryConfig" />
<cache:proxy id="serviceFactory" refId="serviceFactoryTarget">
<cache:caching methodName="get*" cacheName="serviceFactoryTarget" />
Step 4: Pull the Bean definition out of the applicationContext.xml and into its own config with the Spring beans tag and xmlns declarations.
<?xml version="1.0" encoding="UTF-8"?>
<bean id="serviceFactory" class="com.foo.bar.service.ServiceFactory">
<property name="var1" value="foo" />
<property name="var2" value="bar" />
<property name="resourceExt" value="default" />
<property name="var1" value="baz" />
<property name="var2" value="xyz" />
<property name="resourceExt" value="bar" />
And of course you need to define the JNDI values however you want.
So now that the application is configured we can launch the application and once the ServiceFactoryFactoryContextLoader.getServiceFactory() is called the app will make a call out to the externalized location to get the config, load it with the parent context ( which in this case is the applicationContext ) and then you can make the call to get the bean in the externalized config from Step 4 ( "serviceFactory" ). And there it is... the externalized config is now set so that I can host the config where ever I want it; on the local file system somewhere or over http on another server.