Announcement Announcement Module
No announcement yet.
Error in spring context.xml Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 and Image is java.awt.Image. I am trying to configure it via spring as:

    <bean id="a" class="some package">  
        <constructor-arg  type="" 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
         <!--Abstract Class Image  -->  
            <bean id="image" class="java.awt.Image" abstract="true"/>
    and change the above as :
     <bean id="a" class="some package">    
            <constructor-arg  type="" 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).


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

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


      • #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).


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

          For example this editor allow to set images from classpath:

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

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