1. ホーム
  2. java

[解決済み] 一致するエディターやコンバージョン戦略は見つかりませんでした

2022-02-11 16:47:54

質問

私はspringの初心者です。この例外が発生しました。私はググったが、私はこのための正確なソリューションを見つけることができませんでした。

app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    <bean class="com.listinjection.Course" id="course">

        <property name="course">
            <list value-type="java.lang.String">

                <value>c</value>
                <value>c++</value>
                <value>java</value>
                <value>web services</value>

            </list>
        </property>
    </bean>

    <bean class="com.mapinjection.University" id="university">
        <property name="university">
            <map key-type="java.lang.String" value-type="com.mapinjection.Course">
                <entry key="java" value-ref="course">
                </entry>
            </map>
        </property>
    </bean>
</beans>

コース.java

    package com.mapinjection;

import java.util.List;

public class Course {
    private List<String> course;

    public void setCourse(List<String> course) {
        this.course = course;
    }

    public void display(){
        System.out.println("list of courses are:");
        for(String s:course){
            System.out.println(s);
        }
    }

}

大学.java

    package com.mapinjection;

import java.util.Map;

public class University {
    private Map<String, Course> university;

    public void setUniversity(Map<String, Course> university) {
        this.university = university;
    }

    public void display(){
        Course course1=university.get("java");
        course1.display();
    }
}

テスト.java

    package com.mapinjection;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "com/mapinjection/app-context.xml");
        University university=context.getBean("university", University.class);
        university.display();
        context.close();

    }

}

例外

    Jul 25, 2014 10:09:15 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15d2aa14: startup date [Fri Jul 25 10:09:15 IST 2014]; root of context hierarchy
Jul 25, 2014 10:09:15 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/mapinjection/app-context.xml]
Jul 25, 2014 10:09:15 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@614951ff: defining beans [course,university]; root of factory hierarchy
Jul 25, 2014 10:09:15 AM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@614951ff: defining beans [course,university]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'university' defined in class path resource [com/mapinjection/app-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'university'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.listinjection.Course] to required type [com.mapinjection.Course] for property 'university[java]': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.mapinjection.Test.main(Test.java:9)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'university'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.listinjection.Course] to required type [com.mapinjection.Course] for property 'university[java]': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:463)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:494)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1464)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1423)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1159)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.listinjection.Course] to required type [com.mapinjection.Course] for property 'university[java]': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
    at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:648)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:208)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
    ... 17 more

コード全体ではLinkedHashMapを使用していませんが、なぜかquot; Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'university'" と表示されます。

どうすればいいですか?

タイプの値を追加しようとしています。 com.listinjection.CourseMap を期待するものである。 com.mapinjection.Course の値を指定します。

<map key-type="java.lang.String" value-type="com.mapinjection.Course">
    <entry key="java" value-ref="course">

それを修正する。