0

I've been reading questions like this about importing beans from a project containded in a jar file. But what I have in my project (let's call Project A) is just a mvc-config.xml in WEB-INF folder, and application-config.xml in src/main/resources/spring folder (both cometh from the Spring Web Maven template).

I'm defining beans, services and repositories through annotations, in the very classes, and until now, I didn't need any other configuration, like defining beans in mvc-config.xml. This file is, until now, quite spartan, as shown below. But when I pack this project in a jar, imported it in another project (let's call Project B) and try to autowire Dao implementations (in Project B), an error

Severe: Context initialization failed
java.lang.NoClassDefFoundError: com/mycompany/dao/EntityDao

is thrown. Note that this specific class was defined through an interface implementation, which was being autowired in native project very well.

Are these problems occouring because I didn't managed to correctly import Project A in Project B, or do I need to do another kind of arrangement in order to use @Repository, @Entity and other Spring stuff from Project A? What should I do to use that services from Project A in Project B? Thanks!

  • Project B mvc-config.xml

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="com.mycompany"/>
    
    <mvc:annotation-driven />
    
    <context:annotation-config/>
    
    <mvc:default-servlet-handler/>
    
    <import resource="classpath*:/WEB-INF/mvc-config.xml" />
    <import resource="classpath*:/spring/application-config.xml" />
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>
    

  • Stack tracing for thrown error:

Severe: Context initialization failed
java.lang.NoClassDefFoundError: com/mycompany/EntityDao
  at java.lang.Class.getDeclaredMethods0(Native Method)
  at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
  at java.lang.Class.getDeclaredMethods(Class.java:1845)
  at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:571)
  at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:489)
  at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:473)
  at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:534)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:663)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:593)
  at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1394)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:385)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:354)
  at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:97)
  at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
  at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
  at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
  at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
  at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
  at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
  at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
  at org.apache.catalina.core.StandardService.start(StandardService.java:525)
  at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
  at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
  at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.ClassNotFoundException: com.mycompany.EntityDao
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
  ... 33 more
Community
  • 1
  • 1
Alex
  • 3,325
  • 11
  • 52
  • 80
  • 1
    You seem to be on a wrong path here, first of all your project has a mvc-config.xml file which implies that it is a web project project, i have never seen a web project used inside another web project, if you packed project A as jar make sure it contains WEB-INF folder. than make sure that project A jar is available at runtime. Is this a maven project and how you are trying to run this? – varun Sep 25 '14 at 09:18
  • @varun, indeed, I've made kind a "downgrade" in an earlier web project, due to a restructuration of my entire workspace. Departing from your tip, I decided to test the same archytecture starting from scratch, from a Spring-Maven simple project, and everything worked well. In fact it was the reminiscent web structure of my older project that was not fit to be used as a jar. Please, put an answer and I'm going to accept it. Thanks. – Alex Sep 25 '14 at 14:54

1 Answers1

0

You seem to be on a wrong path here, first of all your project has a mvc-config.xml file which implies that it is a web project project, i have never seen a web project used inside another web project, if you packed project A as jar make sure it contains WEB-INF folder. than make sure that project A jar is available at runtime. Is this a maven project and how you are trying to run this?

varun
  • 684
  • 1
  • 11
  • 30