idref标签在春天不起作用

时间:2017-04-08 03:31:23

标签: java spring

我使用idref标签来引用另一个bean,但spring框架抛出了一个类型转换错误,请参阅我的spring.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.xsd">

    <bean id = "triangle" class="org.dxc.javatechnology.Triangle">
        <property name="type" value="Test Value Injection"></property>
        <property name = "c">
            <idref bean= "cycle"/>
        </property>
        <constructor-arg index="0" value="Test Name"></constructor-arg>
        <constructor-arg index = "1" value="20"></constructor-arg>
    </bean>
    <bean id="cycle" class = "org.dxc.javatechnology.Cycle">
        <property name = "radir" value = "5" />
    </bean>
</beans>    

Triangle.java:

package org.dxc.javatechnology;

public class Triangle {
    private String type;
    private Cycle c;
    private String name;
    private int height;

    public int getHeight() {
        return height;
    }
    public Triangle(){}
    public Triangle(String name)
    {
        this.name = name;
    }

    public Triangle(String name,int height)
    {
        this.name = name;
        this.height = height;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Cycle getC() {
        return c;
    }
    public void setC(Cycle c) {
        this.c = c;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public void draw(){
        System.out.println(getType() + " Triangle drawn");
    }
}

Cycle.java

package org.dxc.javatechnology;

public class Cycle {
    private int radir;

    public int getRadir() {
        return radir;
    }

    public void setRadir(int radir) {
        this.radir = radir;
    }
}    

主要方法:

public static void main(String[] args) {
    @SuppressWarnings("resource")
    ApplicationContext context = new ClassPathXmlApplicationContext("ConfigurationFiles/spring.xml");
    Triangle triangle  = (Triangle) context.getBean("triangle");
    triangle.draw();
    System.out.println(triangle.getC().getRadir());
    System.out.println(triangle.getName());
    System.out.println(triangle.getHeight());
}        

错误如下:

Apr 08, 2017 11:16:03 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@497470ed: startup date [Sat Apr 08 11:16:03 CST 2017]; root of context hierarchy
Apr 08, 2017 11:16:03 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ConfigurationFiles/spring.xml]
Apr 08, 2017 11:16:03 AM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [ConfigurationFiles/spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c': no matching editors or conversion strategy found
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [ConfigurationFiles/spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at org.dxc.javatechnology.DrwaingApp.test3(DrwaingApp.java:32)
    at org.dxc.javatechnology.DrwaingApp.main(DrwaingApp.java:10)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c': no matching editors or conversion strategy found
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:605)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:617)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1577)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1536)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    ... 12 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.dxc.javatechnology.Cycle' for property 'c': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
    ... 18 more

我不知道这是什么问题,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

它不是idref只是ref

<property name = "c" ref = "cycle"/>
相关问题