找不到类路径资源 [bean.xml] 中定义的名为“helloworld”的 bean 的类 [com.springdemo]

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

Cannot find class [com.springdemo] for bean with name 'helloworld' defined in class path resource [bean.xml]

xmlspring

提问by Laxmi Kadariya

I am just starting spring framework and tried "Hello world" tutorial from this site. I have Mainapp.Javaas

我刚刚开始使用 spring 框架,并尝试了来自该站点的“Hello world”教程。我有Mainapp.Java作为

package com.springdemo;

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

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context =new ClassPathXmlApplicationContext("bean.xml");
        Hello_World obj = (Hello_World)context.getBean("helloworld");
        obj.getMessage();
        }
}

Hello_World.java

Hello_World.java

package com.springdemo;

public class Hello_World {

    private String message;
    public void setMessage(String message){
    this.message = message;
    }
    public void getMessage(){
    System.out.println("Your Message : " + message);
    }
}

and bean.xml

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">
<bean id="helloworld" class="com.springdemo">
<property name="message" value="Hello World!"/>
</bean>
</beans>

error

错误

Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.springdemo] for bean with name 'helloworld' defined in class path resource [bean.xml]; nested exception is java.lang.ClassNotFoundException: com.springdemo
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:479)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:787)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:393)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:736)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:123)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
    at com.springdemo.MainApp.main(MainApp.java:8)
Caused by: java.lang.ClassNotFoundException: com.springdemo
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:230)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:381)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1170)
    ... 8 more

回答by Kalher

of course @c.P.u1 has already given the answer other way out (auto detection) you can try is

当然@cPu1 已经给出了其他出路(自动检测)的答案,您可以尝试是

  1. Remove your explicit bean declaration

  2. Annotate your class Hello_Worldwith @Component

  3. Use <annotation-driven />in your configuration file

  4. Provide bean detection configuration using <context:component-scan base-package="com.springdemo" />

  1. 删除您的显式 bean 声明

  2. 注释类Hello_World@Component

  3. 使用<annotation-driven />您的配置文件

  4. 使用提供 bean 检测配置 <context:component-scan base-package="com.springdemo" />

回答by Rohit

I added a file extension to the class org.stuff.Triangle.javain the xml file instead of the fully-qualified class name.

org.stuff.Triangle.java在 xml 文件中为类添加了文件扩展名,而不是完全限定的类名。

This is the right way for my code:

这是我的代码的正确方法:

        <bean id="triangle" class="org.stuff.Triangle"/> 

回答by Karim M. Fadel

update bean.xml

更新 bean.xml

<bean id="helloworld" class="com.springdemo.Hello_World">
    <property name="message" value="Hello World!"/>
</bean>

回答by ABDULRAHMAN AL-QADHI

Just make sure to add the class location in bean.xml For your case Hello_World class is the class that return the value so just call this class.

只需确保在 bean.xml 中添加类位置对于您的情况 Hello_World 类是返回值的类,因此只需调用此类即可。

<bean id="helloworld" class="com.springdemo.Hello_World">
   <property name="message" value="Hello World!"/>
</bean>

One more thing I noticed in your code. In the main class You must write the getBean(Bean ID value)

我在您的代码中注意到的另一件事。在主类中你必须写getBean(Bean ID value)

So I think the right one should be:

所以我认为正确的应该是:

Hello_World message= (Hello_World)appContext.getBean("helloworld");

回答by Trevor Good

Beginner here as well and I went through several posts and about 30 min but I am using IntelliJ instead of the recommended IDE. I have the Hello_World and MainApp in src/main/java and the beans in src/main/resources. I finally figured out that the class needed no prefix for location. I know this isn't directly linked to this question but it was top on the google list when searching error messages.

这里也是初学者,我浏览了几篇文章,大约 30 分钟,但我使用的是 IntelliJ 而不是推荐的 IDE。我在 src/main/java 中有 Hello_World 和 MainApp,在 src/main/resources 中有 bean。我终于发现这个类不需要位置前缀。我知道这与这个问题没有直接联系,但在搜索错误消息时它在谷歌列表中名列前茅。

<bean id = "helloWorld" class = "Hello_World">
    <property name = "message" value = "Hello World!"/>
</bean>

回答by DigvijaySingh Chauhan

In the bean tag the class parameter should contain full class name path.You have not written class name. Instead of this bean id="helloworld" class="com.springdemo"

在 bean 标签中,class 参数应该包含完整的类名路径。你没有写出类名。而不是这个 bean id="helloworld" class="com.springdemo"

write this bean id="helloworld" class="com.springdemo.Hello_World"

写这个 bean id="helloworld" class="com.springdemo.Hello_World"