Announcement Announcement Module
Collapse
No announcement yet.
Error in spring context.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error in spring context.xml

    Hello Every One,

    I have class A with constructor argument as (URI uri, Image image), where URI is java.net.URI and Image is java.awt.Image. I am trying to configure it via spring as:

    Code:
    <bean id="a" class="some package">  
        <constructor-arg  type="java.net.URI" value=""/>  
        <constructor-arg type="java.awt.Image" value=""/>
    But error is thrown as : Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'a' defined in class path resource [/context.spring.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [java.awt.Image]: Could not convert constructor argument value of type [java.lang.String] to required type [java.awt.Image]: Failed to convert value of type 'java.lang.String' to required type 'java.awt.Image'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.awt.Image]: no matching editors or conversion strategy found.



    But for image if I create another bean like
    Code:
         <!--Abstract Class Image  -->  
            <bean id="image" class="java.awt.Image" abstract="true"/>
    and change the above as :
    Code:
     <bean id="a" class="some package">    
            <constructor-arg  type="java.net.URI" value=""/>    
            <constructor-arg ref="image"/>
    Then also I am getting errors.

    Please suggest.

  • #2
    Which makes perfect sense...

    1. A string cannot be converted to an java.awt.Image
    2. An abstract bean declaration cannot be referenced nor will it be instantiated.

    What you need to do is create a bean which is the image and put the ref in (as mentioned in 2 this cannot be an abstract one).

    Comment


    • #3
      What if I replace Image with Buffered Image ,. what value will be passed then:

      Code:
      <constructor-arg   type="java.awt.image.BufferedImage" value=""/>

      Comment


      • #4
        I strongly suggest you read the reference guide as you seem to lack some basics on what spring can do for you.

        That wouldn't work either because spring doesn't know how to convert a String to an Image, BufferedImage or whatever java.awt Image subclass you try. YOu need to create the bean and inject it as a ref (as mentioned in my previous post).

        Comment


        • #5
          Use a PropertyEditor to set images as url string in bean definition files

          For example this editor allow to set images from classpath:

          http://sourceforge.net/p/jdal/code/c...rtyEditor.java

          You need to register the editor in context before using it. The following bean definition will register editors for Images and Icons:

          Code:
            <!-- Property Editors -->
                  <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
                          <property name="customEditors">
                          <map>
                                  <entry key="java.awt.Image">
                                          <bean class="info.joseluismartin.beans.ImagePropertyEditor"/>
                                  </entry>
                                  <entry key="javax.swing.Icon">
                                          <bean class="info.joseluismartin.beans.IconPropertyEditor"/>
                                  </entry>
                                  </map>
                          </property>
                  </bean>
          Cheers,

          Comment

          Working...
          X