PDA

View Full Version : Overriding just one bean configuration



evdherberg
Apr 25th, 2005, 05:57 AM
For my unit tests I would like to load the application context in its original form, with just one exception: the datasource bean needs to target a different database. So I'm looking for a way to create a child context with just this one (overriding) bean configuration, with all other bean configurations taken from the parent context.

I have not yet found a way to do this. I can of course create a second datasource bean with id "dataSourceTest", but that would require me to replace the bean references from my Hibernate session factory to "dataSource" with bean references to "dataSourceTest". This in turn would require me to replace references to that session factory etc etc. I would end up copying the complete applicationContext.xml, while it's just one bean that I'd like to replace.

I think I'm overlooking something, as this must be a very common situation. Any clues?

Thanks a lot,
Emiel.

katentim
Apr 25th, 2005, 06:15 AM
Why not use the PropertyPlaceholderConfigurer (http://www.springframework.org/docs/reference/beans.html#beans-factory-placeholderconfigurer).

evdherberg
Apr 25th, 2005, 06:26 AM
Thanks, that is indeed a way out. However I would prefer to have my datasource config in the applicationContext.xml instead of a separate properties file.

I now see (from your link) that the PropertyOverrideConfigurer will allow this - only for the testcases I will have to load the properties file, for the normal application just applicationContext.xml is used.

Thanks again.

dejanp
Apr 25th, 2005, 10:36 AM
Split your context file into 2 - applicationContext.xml and dsContext.xml/testDsContext.xml.

katentim
Apr 25th, 2005, 06:22 PM
Well if you have to replace the values for tests, some options are:
- use PropertyPlaceholderConfigurer and replace the properties file
- split the application context (as dejanp suggests) and replace the dsContext.xml
- use an the Ant replace task to modify the application context
- replace the entire application context (with only datasource modified)

jbetancourt
Apr 26th, 2005, 04:45 AM
I had a similar issue and someone on this forum suggested I simply create another context config file that is loaded in the test case driver. This context cofig file simply redefines the same bean(s) that the normal run environment loads.


Application run:
normalContext.xml
bean Foo ---> normal stuff


Test run:

normalContext.xml
bean Foo ---> normal stuff

testNormalContext.xml
bean Foo ---> test stuff



It works fine. Somewhat analogous to CSS.

raja0719
Apr 29th, 2005, 03:49 PM
I have encountered this problem with a lot of things needing to be changed between Unit Testing and dev environments.

I added the PropertyPlaceHolder but it did not solve the full problem because I did not want to change the properties file for different environments.

So I came up with a variation of this theme which I documented in
http://www.thekollurus.com/blogs

Let me know what you spring gurus think.

Colin Sampaleanu
May 1st, 2005, 10:05 PM
This seems like a viable approach. What I don't quite like about it though is the need to mess around with an environment property. Genrally, when there is a need to set things differently for unit testoing, I find it simplest to simply set things up so that the appcontext is loaded from slightly different xml fragments for produciton use and testing use. You can either have two completely different sets of defintions (for the things that need to be one way or another), or alternately, ensure that for the unit test case, the unit test bean definiton comes in last. In this case, it will override any earlier bean def. of the sam name.