Results 1 to 8 of 8

Thread: Overriding just one bean configuration

  1. #1
    Join Date
    Apr 2005
    Posts
    2

    Default Overriding just one bean configuration

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

  3. #3
    Join Date
    Apr 2005
    Posts
    2

    Default

    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.

  4. #4
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Split your context file into 2 - applicationContext.xml and dsContext.xml/testDsContext.xml.

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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)

  6. #6
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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.
    Code:
    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.

  7. #7
    Join Date
    Mar 2005
    Posts
    3

    Default Property Place holder with an environment variable.

    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.
    Sipping Java with a dash of Spring

  8. #8
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •