gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:10:56 26 4
gpt4 key购买 nike

我刚刚开始使用 spring 框架并尝试了此 site 中的“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

最佳答案

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

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

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

关于xml - 找不到类路径资源 [bean.xml] 中定义的名称为 'helloworld' 的 bean 的类 [com.springdemo],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com