Results 1 to 4 of 4

Thread: setting a dependency on a factory result

  1. #1
    Join Date
    Dec 2004
    Location
    Germany
    Posts
    24

    Default 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. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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?
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Dec 2004
    Location
    Germany
    Posts
    24

    Default

    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

  4. #4
    Join Date
    Dec 2004
    Location
    Germany
    Posts
    24

    Default

    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

Similar Threads

  1. Replies: 1
    Last Post: Jul 28th, 2005, 05:08 PM
  2. Replies: 2
    Last Post: May 13th, 2005, 05:42 AM
  3. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  4. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM
  5. Transaction Management
    By caverns in forum Data
    Replies: 3
    Last Post: Mar 8th, 2005, 06:38 AM

Posting Permissions

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