Announcement Announcement Module
Collapse
No announcement yet.
ref a bean produced by a factory bean from constructor-arg Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ref a bean produced by a factory bean from constructor-arg

    (I'm running the spring.jar and spring-sandbox.jar updated from the Spring RCP CVS as of tonight)
    In my standalone application test setup, I have this in order to load some DBUnit data set:
    Code:
      <bean id="data-set-input-stream" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod"><value>java.lang.ClassLoader.getSystemResourceAsStream</value></property>
        <property name="arguments">
          <list>
            <value>/default-dataset.xml</value>
          </list>
        </property>
      </bean>
    
      <bean id="direct-input-stream" class="java.io.FileInputStream">
        <constructor-arg><value>E&#58;\default-dataset.xml</value></constructor-arg>
      </bean>
    
      <bean id="data-set" class="org.dbunit.dataset.xml.FlatXmlDataSet">
        <constructor-arg type="java.io.InputStream"><ref bean="data-set-input-stream"/></constructor-arg>
        <constructor-arg type="boolean"><value>false</value></constructor-arg>
      </bean>
    Upon loading this app context, I got a bean instantiation error because no matching constructors were found -
    Code:
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.File,boolean&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; Unsatisfied dependency expressed through constructor argument with index 0 of type &#91;java.io.File&#93;&#58; Did you specify the correct bean references as generic constructor arguments?
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.net.URL,boolean&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; Unsatisfied dependency expressed through constructor argument with index 0 of type &#91;java.net.URL&#93;&#58; Did you specify the correct bean references as generic constructor arguments?
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.Reader,boolean&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; Unsatisfied dependency expressed through constructor argument with index 0 of type &#91;java.io.Reader&#93;&#58; Did you specify the correct bean references as generic constructor arguments?
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.Reader,java.io.Reader&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; Unsatisfied dependency expressed through constructor argument with index 0 of type &#91;java.io.Reader&#93;&#58; Did you specify the correct bean references as generic constructor arguments?
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.Reader,org.dbunit.dataset.IDataSet&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; Unsatisfied dependency expressed through constructor argument with index 0 of type &#91;java.io.Reader&#93;&#58; Did you specify the correct bean references as generic constructor arguments?
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.InputStream,boolean&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Failed to convert property value of type &#91;org.springframework.util.MethodInvoker$VoidType&#93; to required type &#91;java.io.InputStream&#93;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.InputStream,java.io.InputStream&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Failed to convert property value of type &#91;org.springframework.util.MethodInvoker$VoidType&#93; to required type &#91;java.io.InputStream&#93;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.InputStream,org.dbunit.dataset.IDataSet&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Failed to convert property value of type &#91;org.springframework.util.MethodInvoker$VoidType&#93; to required type &#91;java.io.InputStream&#93;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;org.xml.sax.InputSource&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; 2 constructor arguments specified but no matching constructor found in bean 'data-set' &#40;hint&#58; specify index arguments for simple parameters to avoid type ambiguities&#41;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.File&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; 2 constructor arguments specified but no matching constructor found in bean 'data-set' &#40;hint&#58; specify index arguments for simple parameters to avoid type ambiguities&#41;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.net.URL&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; 2 constructor arguments specified but no matching constructor found in bean 'data-set' &#40;hint&#58; specify index arguments for simple parameters to avoid type ambiguities&#41;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.Reader&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; 2 constructor arguments specified but no matching constructor found in bean 'data-set' &#40;hint&#58; specify index arguments for simple parameters to avoid type ambiguities&#41;
    04 Nov 2004 22&#58;14&#58;56,953 DEBUG &#91;main&#93; org.springframework.beans.factory.support.DefaultListableBeanFactory  - Ignoring constructor &#91;public org.dbunit.dataset.xml.FlatXmlDataSet&#40;java.io.InputStream&#41; throws java.io.IOException,org.dbunit.dataset.DataSetException&#93; of bean 'data-set'&#58; could not satisfy dependencies. Detail&#58; Error creating bean with name 'data-set' defined in class path resource &#91;global-context.xml&#93;&#58; 2 constructor arguments specified but no matching constructor found in bean 'data-set' &#40;hint&#58; specify index arguments for simple parameters to avoid type ambiguities&#41;
    However if I change the first constructor-arg reference to "direct-input-stream", the bean is instantiated successfully.

    I wonder what I'm missing here?
    Thanks.

  • #2
    You are getting this exception because java.lang.ClassLoader.getSystemResourceAsStream("/default-dataset.xml") returns null.
    Spring has build-in support for resources which actually allows to use the following configuration:
    Code:
    <bean id="data-set" class="org.dbunit.dataset.xml.FlatXmlDataSet"> 
      <constructor-arg type="java.io.InputStream">
        <value>classpath&#58;default-dataset.xml</value>
      </constructor-arg> 
      <constructor-arg type="boolean">
        <value>false</value>
      </constructor-arg> 
    </bean>
    HTH

    Comment


    • #3
      Code:
      <constructor-arg><value>E&#58;\default-dataset.xml</value></constructor-arg>
      Shouldn't that be
      Code:
      E&#58;\\default-dataset.xm 
      or E&#58;/default-dataset.xmll
      ?

      Comment


      • #4
        Originally posted by irbouho
        You are getting this exception because java.lang.ClassLoader.getSystemResourceAsStream("/default-dataset.xml") returns null.
        Spring has build-in support for resources which actually allows to use the following configuration:
        Code:
        <bean id="data-set" class="org.dbunit.dataset.xml.FlatXmlDataSet"> 
          <constructor-arg type="java.io.InputStream">
            <value>classpath&#58;default-dataset.xml</value>
          </constructor-arg> 
          <constructor-arg type="boolean">
            <value>false</value>
          </constructor-arg> 
        </bean>
        That'd be very nice. I'll give it a try as soon as I get the chance. Thanks!

        This leads to another question though - even the ref'ed bean is null, shouldn't it still be able to find the right constructor in this case, since I did give the arg types specifically? I mean, in general, what if null is a legitimate value for a constructor?

        BTW, may I suggest a bit more logging be added to the mismatching constructor message? Currently it only prints out the expected type but not the type of the actual object.

        Comment

        Working...
        X