default-dependency-check ='simple',构造函数注入不起作用

时间:2018-06-18 07:39:16

标签: java spring constructor-injection

我正在学习Spring基础知识,并且长时间陷入与 default-dependency-check 相关的@ 1问题并且无法找到解决方案。请帮忙。

我有3个档案 - DrawingApp .java

public class DrawingApp {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        Triangle traingle = (Triangle) ac.getBean("triangle");
        traingle.draw();
    }}

Triangle.java

public class Triangle {
    String type;
    int height;
    int width;
    Point pointA;
    Point pointB;
    Point pointC;

    public Triangle() {
    }

    Triangle(int height) {
        this.height = height;
    }

    Triangle(String type, int height) {
        this.type = type;
        this.height = height;
    }

    Triangle(String type, int height, int width) {
        this.type = type;
        this.height = height;
        this.width = width;
    }

    Triangle(int height, int width) {
        this.height = height;
        this.width = width;
    }

    public void draw() {
        System.out.println("Triangle PointA is (" + getPointA().getX() + "," + getPointA().getY() + ")");
        System.out.println("Triangle PointB is (" + getPointB().getX() + "," + getPointB().getY() + ")");
        System.out.println("Triangle PointC is (" + getPointC().getX() + "," + getPointC().getY() + ")");
        System.out.println("Triangle " + getType() + "drawn with dimesnions:" + getHeight() + " " + getWidth());
    }

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans default-dependency-check="simple">
    <bean id='triangle' class='org.mayur.spring.Triangle'>
        <constructor-arg index="0" value="Equilateral" />
        <constructor-arg index="1" value="20" />
        <constructor-arg index="2" value="10" />
        <!-- <property name="type" value="Equilateral"></property> <property name="height" 
            value="20"></property> <property name="width" value="10"></property> -->
        <property name="pointA" ref="pointF" />
        <!-- Since these beans are not going to be used anywhere else in application, 
            we made them inner beans (w/o id/name) -->
        <property name="pointB" ref="point2" />
        <property name="pointC" ref="point3" />
    </bean>

    <bean id='pointA' class='org.mayur.spring.Point' name="pointD,pointE pointF">
        <property name="x" value="0" />
        <property name="y" value="0" />
    </bean>

    <bean id='point2' class='org.mayur.spring.Point'>
        <property name="x" value="-20" />
        <property name="y" value="0" />
    </bean>

    <bean id='point3' class='org.mayur.spring.Point'>
        <property name="x" value="0" />
        <property name="y" value="20" />
    </bean>
</beans> 

运行DrawingApp.java给出了以下异常:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through bean property 'height': Set this property value or disable dependency checking for this bean.

如果 -

,它可以正常工作
  1. 我将default-dependency-check =“simple”更改为 default-dependency-check =“none”或“objects”它工作正常。或
  2. 我为'简单'依赖项进行setter注入。 (评论部分 我的spring.xml)
  3. 为什么即使我使用构造函数注入注入了所有必需的“简单”依赖项(到三角形bean),它也会失败?
    这是我关于stackoverflow的第一个问题,所以请原谅错误,如果有的话:):

0 个答案:

没有答案