Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

xml

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

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

and 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$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
like image 463
Laxmi Kadariya Avatar asked Oct 03 '22 15:10

Laxmi Kadariya


2 Answers

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

This is the right way for my code:

        <bean id="triangle" class="org.stuff.Triangle"/> 
like image 122
Rohit Avatar answered Oct 13 '22 11:10

Rohit


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

  1. Remove your explicit bean declaration

  2. Annotate your class Hello_World with @Component

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

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

like image 20
Kalher Avatar answered Oct 13 '22 10:10

Kalher