java NoSuchBeanDefinitionException:未定义名为“name”的 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47757840/
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-03 09:47:38  来源:igfitidea点击:

NoSuchBeanDefinitionException: No bean named 'name' is defined

javaspringspring-mvc

提问by Bhavya Arora

I am trying a very basic example. I tried to see other stack overflow answers, but couldn't solve this problem. I am new to this space, let me know if I left out anything.

我正在尝试一个非常基本的例子。我试图查看其他堆栈溢出答案,但无法解决此问题。我是这个空间的新手,如果我遗漏了什么,请告诉我。

Here is my error log :

这是我的错误日志:

Dec 11, 2017 8:40:20 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [BeanDefinition.xml] Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MySpringBeanWithDepenency' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) at model.main.Main.main(Main.java:18)

2017 年 12 月 11 日上午 8:40:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:从类路径资源加载 XML bean 定义 [BeanDefinition.xml] 线程“main”org.springframework.beans.factory 中的异常.NoSuchBeanDefinitionException: 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalAbtractBeanFactory() 处没有定义名为“MySpringBeanWithDepenency”的 bean :1159) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) at model.main.Main.main(Main.java:18)

which clearly says it cannot find the MySpringBeanWithDepenency

这清楚地表明它找不到 MySpringBeanWithDepenency

Here is my BeanDefinition.xml

这是我的 BeanDefinition.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="model.testbean" />
<context:component-scan base-package="model.writer" />

this is my main class :

这是我的主要课程:

package model.main;

/**
 * Created by barora on 12/10/2017.
 */

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import model.testbean.MySpringBeanWithDepenency;

public class Main {
    public static void main(String args[]){
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "BeanDefinition.xml");
        BeanFactory factory = context;
        MySpringBeanWithDepenency test = (MySpringBeanWithDepenency) factory.getBean("MySpringBeanWithDepenency");
        test.run();
    }
}

this is my MySpringBeanWithDepenency

这是我的 MySpringBeanWithDepenency

package model.testbean;

/**
 * Created by barora on 12/10/2017.
 */

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import model.writer.IWriter;
import org.springframework.stereotype.Service;


@Service
public class MySpringBeanWithDepenency {

    private IWriter writer;

    @Autowired
    public void setWriter(IWriter writer){
        this.writer = writer;
    }

    public void run(){
        String s = "This is my test";
        writer.writer(s);
    }
}

this is my folder structure :

这是我的文件夹结构:

enter image description here

在此处输入图片说明

回答by slim

Try to debug adding:

尝试调试添加:

System.out.println("Bean names: " + Arrays.toString(context.getBeanNamesForType(MySpringBeanWithDependency.class)));

If this prints an empty list, then you have an issue with your classpath scanning. If it prints a name, then that's a name you can use to get the bean.

如果这会打印一个空列表,那么您的类路径扫描存在问题。如果它打印一个名称,那么该名称就是您可以用来获取 bean 的名称。

Alternatively, instead of factory.getBean(String name)use factory.getBean(MySpringBeanWithDependency.class), so that you don't need to know what name Spring has given your bean.

或者,不要factory.getBean(String name)使用 use factory.getBean(MySpringBeanWithDependency.class),这样您就不需要知道 Spring 给您的 bean 起了什么名字。

回答by alexdzot

Try MySpringBeanWithDepenency test = (MySpringBeanWithDepenency) factory.getBean(MySpringBeanWithDepenency.class);

尝试 MySpringBeanWithDepenency test = (MySpringBeanWithDepenency) factory.getBean(MySpringBeanWithDepenency.class);

EDITED

已编辑

So I've recreated code using given project structure. It works with exception that I've used following code for IWriter

所以我使用给定的项目结构重新创建了代码。它适用于我使用以下代码的例外情况IWriter

  package model.writer;

    public interface IWriter {

        void writer(String s);
    }

and for implementation

和实施

package model.writer;

import org.springframework.stereotype.Component;

@Component
public class NiceWriter implements IWriter {
    public void writer(String s) {

    }
}

EDITED 2If you want to have two implementation for IWriterinterface and both supposed to be spring managed beans you need to help spring container to decide which one you want it to autowire. Use @Qualifierfor this. See tutorialfor details

编辑 2如果您想为IWriter接口提供两个实现并且都应该是 spring 管理的 bean,您需要帮助 spring 容器决定您希望它自动装配哪一个。@Qualifier为此使用。请参见教程详细内容

回答by Abdelouahed ECH CHRIF

The default bean name is mySpringBeanWithDepenency with "m" you should use

默认 bean 名称是 mySpringBeanWithDepenency,您应该使用“m”

MySpringBeanWithDepenency test = (MySpringBeanWithDepenency)factory.getBean("mySpringBeanWithDepenency");