Results 1 to 5 of 5

Thread: Overriding a Bean definition

  1. #1
    Join Date
    Sep 2004
    Posts
    2

    Default Overriding a Bean definition

    Hello,

    I tried to search for the answer, but didn't find it...so Trying it out here.

    I have a XMLBeanFactory (A) used for some purpose, and then I have another XMLBeanFactory (B) used in a different context. For some reason, I would like to read a Bean definition from Factory B, and place the same definition, at the same key in Factory (A) (with the problem that my singleton in factory (A) may have already been created).

    I didn't find a way to do this, even by reading the Bean definition in (B) and using the registerBeanDefinition(beanName, beanDefinition) method on (A).

    Is this possible to do it ?

    Thanks a lot

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Ssmeki,
    You can share a bean instance between two XmlBeanFactory by using a parent XmlBeanFactory:
    - P (parent XmlBeanFactory): contains the shared bean you want to use in A and B
    - A: remove definition of your shared bean
    - B: remove definition of your shared bean

    When you instanciate A and B, provide P as the value of parameter parentBeanFactory

    Code:
      XmlBeanFactory p = new XmlBeanFactory(isP);
      XmlBeanFactory a = new XmlBeanFactory(isA, p);
      XmlBeanFactory b = new XmlBeanFactory(isB, p);
    XmlBeanFactory A and B will inherits your shared bean definition / instance from P.

    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

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

    Default

    Note also that ContextLoader is set up so that it may be easily subclassed to provide a parent to the main web application context that is normally created by itself. This allows you for example to use something like a SingletonBeanFactoryLocator to instantiate the 'main' application context(s), and then the web one is just a child of it.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  4. #4
    Join Date
    Sep 2004
    Posts
    2

    Default

    Thank you both for your answers,

    Yes I knew about the parent factory, but my case is different. In fact, I really want at some point to change the behaviour of a Factory. In fact override its definition. Maybe the logic is wrong to do such a thing.

    But its true, that it is at least interesting, technically speaking, to know if it is possible to do it....

  5. #5
    Join Date
    Aug 2004
    Location
    Vancouver, Canada
    Posts
    25

    Default

    You can use HotSwappableTargetSource. You have to call swap() method whenever you want to swap the implemenation.

    I'm assuming both your beans implement a common interface.

    Ram.

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
  •