java 春天没有这样的bean定义异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17017788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:43:53  来源:igfitidea点击:

no such bean definition exception in spring

javaspringexception

提问by The good the bad the ugly

I am new to the concepts of Spring and Hibernate. I am using collections in my simple spring program. My idea is to store an author name, address and id into collection. It's a simple spring framework but I am struck with one error.

我对 Spring 和 Hibernate 的概念很陌生。我在简单的 spring 程序中使用集合。我的想法是将作者姓名、地址和 id 存储到集合中。这是一个简单的 spring 框架,但我对一个错误感到震惊。

spring-model.xml

弹簧模型.xml

<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-2.5.xsd">

    <bean id="book" class="com.vishal.Book">
        <property name="author ">
            <list>
                <ref bean="book1 " />
                <ref bean="book2 " />
                <ref bean="book3 " />    
            </list>
        </property>
    </bean>

    <bean id="book1" class="com.vishal.Book">
        <property name="name" value="mark twain" />
        <property name="address" value="London" />
        <property name="id" value="230" />
    </bean>

    <bean id="book2" class="com.vishal.Book">
        <property name="name" value="gutav friedman" />
        <property name="address" value="Germany" />
        <property name="id" value="231" />
    </bean>

    <bean id="book3" class="com.vishal.Book">
        <property name="name" value=" Erastothe " />
        <property name="address" value="spain" />
        <property name="id" value="232" />
    </bean>

</beans>

main class

主班

package com.vishal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Springmain {

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring-model.xml");
        Book book = (Book) context.getBean("book");
        book.authorName();
    }

}

book.java

书.java

package com.vishal;

import java.util.List;

public class Book {

    private List<Author> author;

    public List<Author> getAuthor() {
        return author;
    }

    public void setAuthor(List<Author> author) {
        this.author = author;
    }

    public void authorName() {
        for (Author authors : author)
            System.out.println(authors.getAddress() + authors.getId()
                    + authors.getName());
    }
}

author class

作者班

package com.vishal;

public class Author {
    private String name;
    private String address;
    private int id;

    public String getAddress() {
        return address;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

Following is the error which I a getting

以下是我得到的错误

Jun 10, 2013 11:29:36 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@161d36b: startup date [Mon Jun 10 11:29:36 IST 2013]; root of context hierarchy
Jun 10, 2013 11:29:36 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-model.xml]
Jun 10, 2013 11:29:37 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b5998f: defining beans [book,book1,book2,book3]; root of factory hierarchy
Jun 10, 2013 11:29:37 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b5998f: defining beans [book,book1,book2,book3]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'book' defined in class path resource [spring-model.xml]: Cannot resolve reference to bean 'book1 ' while setting bean property 'author ' with key [0]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'book1 ' is defined
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    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.vishal.Springmain.main(Springmain.java:10)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'book1 ' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
    ... 17 more

采纳答案by Luiggi Mendoza

In your Bookclass you have a List<Author>but in your spring-model.xmlfile you refer to book1, book2and book3as instances of Book, not from Authorprobably due for copy/paste process and forgetting the most important step: adapt what you pasted.

在您的Book班级中,您有一个List<Author>but 在您的spring-model.xml文件中引用book1book2并且book3作为 的实例Book,而不是Author可能由于复制/粘贴过程而忘记了最重要的步骤:调整您粘贴的内容

Just to show an example:

只是为了展示一个例子:

<bean id="book1" class="com.vishal.Book">

It should be

它应该是

<bean id="book1" class="com.vishal.Author">

Also, you're referring to other beans using

此外,您指的是其他豆类使用

<ref bean="book1 " />

There's a white space in the end that should be trimmed. It should be

最后有一个空白应该修剪。它应该是

<ref bean="book1" />

Similar for other bookXreferences in your spring-model.xmlfile.

spring-model.xml文件中的其他bookX引用类似。

回答by Harish Kumar

There is space in bean references:

bean 引用中有空格:

whereas actual bean name has no space:

而实际的 bean 名称没有空格:

Also as Luiggi suggested Bean definition needs to corrected.

此外,Luiggi 建议 Bean 定义需要更正。

Please check this.

请检查这个。