如何解决Spring UnsatisfiedDependencyException错误?

时间:2017-09-19 19:40:31

标签: java spring

它应该输出spring.xml文件中输入的三角形的坐标(x,y)。

这是drawingapp。 java类

 package spring_java_brains_07_injecting_objects;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

public class drawingapp {

public static void main(String[] args) {

    //triangle t = new triangle();
    //BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    triangle t = (triangle) context.getBean("test");
    t.draw();

}

}

这是triangle.java

package spring_java_brains_07_injecting_objects;

import java.awt.Point;

public class triangle {

private Point a;
private Point b;
private Point c;

public Point getA() {
    return a;
}

public Point getB() {
    return b;
}

public Point getC() {
    return c;
}

public triangle(Point aaa, Point bbb, Point ccc) {

    this.a = aaa;
    this.b = bbb;
    this.c = ccc;

}

public void draw() {

    System.out.println(getA().getX() + " " + getA().getY());
    System.out.println(getB().getX() + " " + getB().getY());
    System.out.println(getC().getX() + " " + getC().getY());

}

}

这是point.java类

package spring_java_brains_07_injecting_objects;

public class point {

private int x;
private int y;

public int getX() {
    return x;
}
/*public void setX(int xxx) {
    this.x = xxx;
}*/
public int getY() {
    return y;
}
/*public void setY(int yyy) {
    this.y = yyy;
}*/

public point(int xxx, int yyy) {

    this.x = xxx;
    this.y = yyy;

}

}

最后这是spring.xml文件

<bean id="test" class="spring_java_brains_07_injecting_objects.triangle">

    <constructor-arg ref="zero_point" />
    <constructor-arg ref="one_point" />
    <constructor-arg ref="two_point" />

</bean>

<bean id="zero_point" class="spring_java_brains_07_injecting_objects.point">

    <constructor-arg type="int" value="0" />
    <constructor-arg type="int" value="0" />

</bean>

<bean id="one_point" class="spring_java_brains_07_injecting_objects.point">

    <constructor-arg type="int" value="-20" />
    <constructor-arg type="int" value="0" />

</bean>

<bean id="two_point" class="spring_java_brains_07_injecting_objects.point">

    <constructor-arg type="int" value="0" />
    <constructor-arg type="int" value="20" />

</bean>

如果有人能告诉我这里我做错了什么?非常感谢你提前。

,错误是:

Sep 19, 2017 9:22:30 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@439f5b3d: startup date [Tue Sep 19 21:22:30 CEST 2017]; root of context hierarchy
Sep 19, 2017 9:22:30 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Sep 19, 2017 9:22:30 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@64cee07: defining beans [test,zero_point,one_point,two_point]; root of factory hierarchy
Sep 19, 2017 9:22:30 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@64cee07: defining beans [test,zero_point,one_point,two_point]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.awt.Point]: Could not convert constructor argument value of type [spring_java_brains_07_injecting_objects.point] to required type [java.awt.Point]: Failed to convert value of type 'spring_java_brains_07_injecting_objects.point' to required type 'java.awt.Point'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [spring_java_brains_07_injecting_objects.point] to required type [java.awt.Point]: no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:702)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at spring_java_brains_07_injecting_objects.drawingapp.main(drawingapp.java:15)

1 个答案:

答案 0 :(得分:1)

查看班级名称和导入。在三角形类中,您使用的是java.awt.Point类而不是您自己的类。

异常非常清楚

Unsatisfied dependency expressed through constructor argument with index 0 of type [java.awt.Point]: Could not convert constructor argument value of type [spring_java_brains_07_injecting_objects.point] to required type [java.awt.Point]: Failed to convert value of type 'spring_java_brains_07_injecting_objects.point' to required type 'java.awt.Point'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [spring_java_brains_07_injecting_objects.point] to required type [java.awt.Point]: no matching editors or conversion strategy found

它告诉你他无法将spring_java_brains_07_injecting_objects.point投放到java.awt.Point

只需修改三角形类中的导入即可使用自定义类,它应该可以正常工作。