线程" main"中的例外情况org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为' std'的bean时出错。在文件中定义

时间:2015-03-11 05:59:04

标签: spring

package com.nareshit.beans;

public class Address {
    private String city,state;
    public String toString()
    {
    return "\n city:"+city+"\nstate:"+state;    
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }

}

package com.nareshit.clients;

import java.awt.Container;
import java.beans.Beans;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.nareshit.beans.Student;

public class Test {
    public static void main(String[] args)
    {
        BeanFactory factory=new XmlBeanFactory(new ClassPathResource("MyBeans.xml"));

        Student std1=(Student)factory.getBean("std");

        std1.getStudentDetails();
    }
}


package com.nareshit.beans;

public class Student {
    private int sid;
    private String name;
    private String address;

    public void getStudentDetails()
        {
        System.out.println("student id:"+sid);
        System.out.println("student name:"+name);
        System.out.println("student address:"+address);
        }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
                "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>
    <bean id="std" class="com.nareshit.beans.Student">
    <property name="std" value="1001"/>
    <property name="name" value="mitu"/>
    <property name="address" ref="addressObj"/>
    </bean>

    <bean id="addressObj" class="com.nareshit.beans.Address">
    <property name="city" value="bam"/>
    <property name="state" value="odisha"/>
    </bean>
    </beans>

这是弹簧定位器注射程序。我在哪里得到错误:

  

线程“main”中的异常org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件(“”)中定义名称为“std”的bean时出错

如何解决?

1 个答案:

答案 0 :(得分:0)

请参阅 com.nareshit.beans.Student 课程。它有一个名为 address 的属性,它是一种 String 。在编写XML时, 您提供的参考类型为 com.nareshit.beans.Address

解决方案:  1.您可以在XML中为地址property.like

指定一个String值
ddress property like
       <bean id="std" class="com.nareshit.beans.Student">
    <property name="std" value="1001"/>
    <property name="name" value="mitu"/>
    <property name="address" value="SOME ADDRESS"/>
    </bean>

2。您可以在学生班中更改地址属性的类型,如

public class Student{
    Address address;
    //declaration of rest property
//Some Boiler Plate Code
}