Results 1 to 5 of 5

Thread: Error in spring context.xml

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    Default

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

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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    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,
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

Tags for this Thread

Posting Permissions

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