从jar导入Spring Beans

时间:2014-09-24 19:51:56

标签: java spring spring-mvc

我一直在阅读像this这样的问题,关于从包含在jar文件中的项目导入bean。但是我在项目中的内容(让我们称之为Project A)只是WEB-INF文件夹中的mvc-config.xml和src / main / resources / spring文件夹中的application-config.xml(都来自Spring Web Maven)模板)。

我通过注释在类中定义bean,服务和存储库,直到现在,我不需要任何其他配置,比如在mvc-config.xml中定义bean。到目前为止,这个文件非常简洁,如下所示。但是当我将这个项目打包在一个jar中时,将它导入另一个项目(让我们调用Project B)并尝试自动装载Dao实现(在Project B中),这是一个错误

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

被抛出。请注意,此特定类是通过接口实现定义的,该接口实现非常适合在本机项目中自动装配。

这些问题是否存在,因为我没有设法在项目B中正确导入项目A,或者我是否需要进行另一种安排才能使用项目A中的@ Repository,@ Entity和其他Spring内容?如何在项目B中使用项目A中的服务?谢谢!

  • 项目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>
    

  • 抛出错误的堆栈跟踪:

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

1 个答案:

答案 0 :(得分:0)

你似乎在这里错误的路径,首先你的项目有一个mvc-config.xml文件,暗示它是一个web项目项目,我从未见过在另一个web项目中使用的web项目,如果你把项目A打包成jar,确保它包含WEB-INF文件夹。确保项目A jar在运行时可用。这是一个maven项目以及你如何尝试运行它?