Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring NoClassDefFoundError org.springframework.beans.FatalBeanException when adding <context:component-scan ...> in the config

Tags:

java

spring

I'm writing a simple Spring 3.1 test, I'm getting an exception when adding the following line in my config:

<context:component-scan base-package="com.software.shared" />

Here's the exception:

INFO: Loading XML bean definitions from class path resource [spring-config.xml]
Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.software.shared.PersonBeanTest.<init>(PersonBeanTest.java:15)
at com.software.shared.PersonBeanTest.main(PersonBeanTest.java:31)

I don't get what's going on. If I remove the line, the exception dissappears, but Autowiring doesn't work.

I have all the jars in the spring 3.1 RELEASE distribution, on my class path, including org.springframework.beans-3.1.0.RELEASE.jar and I checked that it contains that file. This is the code in the main method:

package com.software.shared;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

@Service
public class PersonBeanTest {

    public PersonBeanTest() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        //System.out.println("Name=" + ((PersonBean) (context.getBean("personBean"))).getName());
    }

    private PersonBean myBean;

    public PersonBean getMyBean() {
        return myBean;
    }

    @Autowired
    public void setMyBean(PersonBean myBean) {
        this.myBean = myBean;
    }

    public static void main(String[] args) {
        PersonBeanTest test = new PersonBeanTest();
        System.out.println("Name=" + test.getMyBean().getName());
    }
}

Here's the Spring config:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">

    <!-- <context:component-scan base-package="com.software" /> -->
    <!-- Register Annotation-based Post Processing Beans -->
    <context:annotation-config />

    <!-- Scan context package for any eligible annotation configured beans. -->
    <context:component-scan base-package="com.software.shared" />

    <bean id="personBean" class="com.software.shared.PersonBean">
        <property name="name" value="MyName" />
    </bean>

</beans>

I start the app by right clicking on this class and clicking "Run As -> Java Application". Any ideas on why I get the exception?

like image 273
user361676 Avatar asked Jan 17 '23 23:01

user361676


1 Answers

you are trying to make spring scan class PersonBeanTest in package com.software.shared, but meanwhile you are creating the same application context in the constructor of PersonBeanTest, I guess this is the root cause of the exception.

try to scan some other package:

<context:component-scan base-package="some.other.package" />

and test it with the following code snippet to see if it works

public class PersonBeanTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        assertNotNull(context.getBean(PersonBean.class));
    }

}
like image 159
Septem Avatar answered Jan 31 '23 10:01

Septem