5

我刚刚开始使用 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

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);
    }
}

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>

错误

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$1.run(Unknown Source)
    at java.net.URLClassLoader$1.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
4

6 回答 6

2

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

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

        <bean id="triangle" class="org.stuff.Triangle"/> 
于 2014-02-08T06:20:00.793 回答
2

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

  1. 删除您的显式 bean 声明

  2. Hello_World用注释你的类@Component

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

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

于 2013-10-22T05:14:11.610 回答
0

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

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

于 2019-08-31T06:49:36.483 回答
0

更新 bean.xml

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

于 2016-10-09T10:27:17.413 回答
0

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

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

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

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

Hello_World message= (Hello_World)appContext.getBean("helloworld");
于 2017-07-03T06:37:28.170 回答
0

这里也是初学者,我浏览了几篇文章大约 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>
于 2017-11-26T02:22:37.423 回答