Results 1 to 5 of 5

Thread: A depends on B depends on A

  1. #1

    Default A depends on B depends on A

    Looks like a circular dependency and for bean factory purposes it is (but because of the class hierarchy actually it isn't in fact).

    Problem is that A needs to have B injected into it and vice versa.

    The way I've solved it is:

    1. declare A.
    2. declare B and inject A. B then injects itself into A (programmatically).

    Is there a better way of doing this in Spring?

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    That could not be solved in a straightforward manner.
    How would you do it programmatically? You would need a component which instantiates both classes and set the references to each other afterwards.

    In contrast to this, the approach in spring is to finish one bean a time (instantiation + setting of properties). Hence, circular dependencies are problematic.

    I can propose you following approaches:
    1. Implement your setters so that they synchronizes themselves. If you assign an A to a B the backward reference will be set automatically. I guess this is similar to your current approach.

    2. You can use lookup-method injection for one of the classes (see reference documentation chapter 3.3.3.1)

    3. You can use a BeanPostProcessor which sets the backward references of dedicated beans.

    Hope that helps,
    Andreas

  3. #3

    Default

    Hi Andreas

    Thanks for the pointers, very interesting I'll have to go have a look at your 2 & 3.

    Cheers

    Edward

  4. #4
    Join Date
    Nov 2004
    Location
    Los Angeles, CA
    Posts
    5

    Default Lookup-Method Injection worked great

    Following Andreas's advice, I used lookup-method injection, and it worked great. In my case, I have a bean that needs to call another of its methods (same bean) through its own transaction proxy. Not exactly as your case, Edward, but still a circular reference.

    Since this is a singleton bean, and LMI is activated on every call (returning the same reference), I created a private member "self" and an associated accessor pair. In the getter, I check if self is null, and if so call the LMI method "getProxiedSelf". This way the proxy is looked up only once and cached in the member variable.

    Thanks guys for asking and answering my question!

  5. #5
    Join Date
    Jan 2005
    Posts
    1

    Default

    I've also been grappling with this problem, and went with solution 3 for the case of a number of beans which need depend on, and are ultimately depended on by, a JFrame. Using this approach I can do a whole batch at once. I'm just finding my feet with Spring though, does this seem sensible?

    public class NeedsFramePostProcessor implements BeanFactoryPostProcessor {

    private final JFrame frame;

    public NeedsFramePostProcessor(JFrame frame) {
    this.frame = frame;
    }

    public void postProcessBeanFactory(ConfigurableListableBeanFac tory beanFactory)
    throws BeansException {
    Map beans = beanFactory.getBeansOfType(NeedsFrame.class, false, false);
    for (Iterator iter = beans.values().iterator(); iter.hasNext() {
    NeedsFrame element = (NeedsFrame) iter.next();
    element.setFrame(frame);
    }
    }
    }

Similar Threads

  1. Why depends spring on hibernate 2.1?
    By hiberman in forum Data
    Replies: 2
    Last Post: Aug 23rd, 2005, 02:15 PM
  2. Does Spring 1.1 depends on Xerces ?
    By antonypaul in forum Container
    Replies: 7
    Last Post: Jun 3rd, 2005, 07:30 AM
  3. PropertyResourceConfigurer depends on another
    By ketan in forum Container
    Replies: 3
    Last Post: Dec 23rd, 2004, 07:20 PM
  4. NameNotFoundException: ejb not bound
    By alesj in forum EJB
    Replies: 2
    Last Post: Oct 19th, 2004, 11:55 AM
  5. Replies: 6
    Last Post: Sep 7th, 2004, 12:22 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
  •