Announcement Announcement Module
Collapse
No announcement yet.
setting a dependency on a factory result Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • setting a dependency on a factory result

    I have a somewhat tricky problem setting a dependency on a factory that has no default constructor but a getInstance method

    Code:
    <bean id="sampleFactory"
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
      <property name="targetClass"><value>sample.SampleFactory</value></property>
      <property name="targetMethod"><value>getInstance</value></property>
    </bean>
    
    <bean id="specialStuff"
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
      <property name="targetObject"><ref bean="sampleFactory"/></property>
      <property name="targetMethod"><value>getSpecialStuff</value></property>
    </bean>
    I want to inject a dependency on the sampleFactory after I created it with getInstance(). My last try was a replacement of the first bean definition with this one, but it completely ignores the dependency:

    Code:
    <bean id="sampleFactory" class="sample.SampleFactory" factory-method="getInstance">
      <property name="dependency"><ref bean="dependencyBean"/></property>
    </bean>
    The easiest way would be to make the constuctor of SampleFactory public, but there are some reasons not to do this...

    Does someone have any hints for this problem?

    Felix

  • #2
    Code:
    <bean id="sampleFactory" class="sample.SampleFactory" factory-method="getInstance"> 
      <property name="dependency"><ref bean="dependencyBean"/></property> 
    </bean>
    This should definitely work, assuming you have an appropriate setDependency method. Can you please try to create a simplified version of the problem and raise it as a JIRA issue?

    Comment


    • #3
      Thanks, Rod! Spring works perfectly as You say, a simplified example proofed that.

      I was tricked by the factory: it uses the dependency in the factory method getInstance() but the dependency is set afterwards... a typical case of the "select is broken" phaenomen

      Felix

      Comment


      • #4
        Instead of passing the dependency with <property>, I added a factory method with the dependency parameter and use <constructor-arg>, so it looks like this:

        Code:
        <bean id="sampleFactory" class="sample.SampleFactory" factory-method="getInstance">
          <constructor-arg><ref bean="dependencyBean"/></constructor-arg>
        </bean>
        Spring passes the constructor-arg to the factory method... great stuff!

        Felix

        Comment

        Working...
        X