Results 1 to 4 of 4

Thread: ref a bean produced by a factory bean from constructor-arg

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default 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.
    --Jing Xue

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Toronto
    Posts
    8

    Default

    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
    ?

  4. #4
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Quote 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.
    --Jing Xue

Similar Threads

  1. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 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
  •