Announcement Announcement Module
Collapse
No announcement yet.
linking two beans to eachother? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • linking two beans to eachother?

    Hellu,

    How can I link A to B and B to A?
    I know this sounds weird and you probably think this is a design mistake. But it is correct.

    The problem I get is that I get an endless loop between the constructors..

    What I really have is:
    I have bean A and B and they have both the same parent C.
    D is a dynamic proxy that included A and B such that anyones accesses A and B through D. However to make sure that A can access B, C also contains a property field that contains D.

    So what happens is that it first encounters D in the xml and tries to create it, it then encounters A en tries to create it as well. It sees that A extends C and so it tries to create C. However, C has a reference to D, and so it starts from the beginning again.

    Where I use this?.. between the different application layers.

    Please some advice?

    Cheers,
    Ed
    PS: I am using spring 1.2

  • #2
    One possibility is to adjust the setter to take care of the reverse side also. That means when invoking D.setA(A a) internally A.setD(this) will be invoked to keep the association in sync.

    Another option is using a BeanPostProcessor which takes care of performing the wiring between the beans.

    Regards,
    Andreas

    Comment


    • #3
      Thanks, I will give it a try.
      But if I read the other replies about circular dependencies, I got the impression that my situation is possible.
      So, why doesn't it work ?

      Cheers,
      Ed

      Comment


      • #4
        Under some conditions Spring might indeed be able to resolve circular dependencies. Though, as I understand, you have a dependency between constructors which makes things problematic as you need both instances before you could construct them. Using setters might get this working as the instantiation and initialization steps are separated.

        Regards,
        Andreas

        Comment


        • #5
          Thanks for your quick response.

          Hmmmm interesting as I use setters.
          Let me post a bit of the xml below.
          Below:
          - The ServiceProxy bean is the proxy.
          - BaseService bean is the parent of the postCodeServiceImpl and templateServiceImpl bean that are included in the proxy

          Maybe I am just doing something weird... but can't see it...

          What happens below is that he get stuck (endless loop) in the constructor of postCodeServiceImpl.

          Thanks,
          Ed
          ----

          <!-- ================================================== =========================== -->
          <!-- Dynamic Service proxy -->
          <!-- ================================================== =========================== -->
          <bean id="serviceProxy" init-method="init" class="bla.common.util.proxy.impl.ProxyMethodDefau lt" singleton="false">
          <property name="interfaceName" value="bla.lidmaatschap.services.ServiceProxy" />
          <property name="implementers">
          <list>
          <!-- ================================================== =========================== -->
          <!-- The postcode service -->
          <!-- ================================================== =========================== -->
          <bean id="postCodeServiceImpl" class="bla.anwbnl.lidmaatschap.services.impl.Servi cePostCodeDefault" parent="baseService">
          <constructor-arg type="java.lang.String" value="${service.postcode.url}" />
          </bean>

          <!-- ================================================== =========================== -->
          <!-- The Template service -->
          <!-- ================================================== =========================== -->
          <bean id="templateServiceImpl" class="bla.lidmaatschap.services.impl.ServiceTempl ateDefault" parent="baseService">
          <property name="jspTemplateService">
          <bean id="jspTemplateService" name="jspTemplateService" class="org.springframework.remoting.httpinvoker.Ht tpInvokerProxyFactoryBean">
          <property name="serviceUrl" value="${service.jspTemplate.url}" />
          <property name="serviceInterface" value="${service.jspTemplate.interface}" />
          </bean>
          </property>
          <property name="properties" ref="propertyConfigurer" />
          <property name="refreshTimeInMilli" value="${cms.template.cache.refresh}" />
          </bean>
          </list>
          </property>
          </bean>

          <!-- ================================================== =========================== -->
          <!-- The Base service -->
          <!-- ================================================== =========================== -->
          <bean id="baseService" class="bla.lidmaatschap.services.impl.ServiceBase" abstract="true">
          <property name="service" ref="serviceProxy" />
          <property name="dao" ref="daoProxy" />
          <property name="xslTranformerType" value="${xsl.tranformer.type}" />
          </bean>

          Comment


          • #6
            The problem is, that the proxy is a prototype (singleton="false").

            You create a "serviceProxy" which creates the inner "postCodeServiceImpl" bean. That in turn is a "baseService", so it refers back to "serviceProxy". Since "serviceProxy" is a prototype you get a _new_ instance and the cycle begins again.

            If you cannot get rid of the prototype, you might need to refine your class dependencies a bit. Maybe a BeanPostProcessor might also help.

            Regards,
            Andreas

            Comment


            • #7
              Hmmmmmm Thanx a lot... You just fixed me a bug.. so I owe you a beer ...

              I completely missed that... was looking in the wrong direction.

              Thanx,
              Ed

              Comment


              • #8
                Originally posted by edbras View Post
                Hmmmmmm Thanx a lot... You just fixed me a bug.. so I owe you a beer ...
                You are welcome (and the beer is too )

                Andreas

                Comment

                Working...
                X