错误 org.springframework.web.servlet.DispatcherServlet - 上下文初始化失败

Posted

技术标签:

【中文标题】错误 org.springframework.web.servlet.DispatcherServlet - 上下文初始化失败【英文标题】:ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed 【发布时间】:2016-06-09 13:18:52 【问题描述】:

我已经在我的项目中正确地映射了每个 bean,以便提供相应的路径。该项目在它创建的机器上运行良好。但是当其他开发人员导入项目并在成功清理和安装后尝试运行它时,它会抛出异常。此外,我查看了一些类似标题的问题,但没有一个对我有用。

注意:DAO 层或数据库层作为单独的项目创建并作为 jar 包含到项目中。

例外:

2016-02-26 15:09:33,709 [localhost-startStop-1] INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'artistController' with a different definition: replacing [Generic bean: class [com.ceelive.controller.ArtistController]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\ceelive-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ceelive\WEB-INF\classes\com\ceelive\controller\ArtistController.class]] with [Generic bean: class [com.ceelive.controller.ArtistController]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in ServletContext resource [/WEB-INF/servlet/ceeliveservlet/servlet-context.xml]]
2016-02-26 15:09:33,921 [localhost-startStop-1] WARN  org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'artistController' defined in ServletContext resource [/WEB-INF/servlet/ceeliveservlet/servlet-context.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/ceelive/dao/manager/artist/ArtistDao
2016-02-26 15:09:33,921 [localhost-startStop-1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'artistController' defined in ServletContext resource [/WEB-INF/servlet/ceeliveservlet/servlet-context.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/ceelive/dao/manager/artist/ArtistDao
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
 at javax.servlet.GenericServlet.init(GenericServlet.java:158)
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: com/ceelive/dao/manager/artist/ArtistDao
 at java.lang.Class.getDeclaredMethods0(Native Method)
 at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
 at java.lang.Class.getDeclaredMethods(Class.java:1975)
 at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
 at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
 at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanF...

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cimple</groupId>
  <artifactId>ceelive</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ceelive Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

        <!-- Spring Dependencies-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.4.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Need this for json to/from object -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.1.0</version>
        </dependency>

        <!-- jstl Servlet -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>

        <!-- Logging -->
        <!-- <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.14</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency> -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.14</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.14</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.14</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>
        <!-- Apache tiles -->
        <dependency>
           <groupId>org.apache.tiles</groupId>
           <artifactId>tiles-api</artifactId>
           <version>2.2.2</version>
        </dependency>
        <dependency>
           <groupId>org.apache.tiles</groupId>
           <artifactId>tiles-core</artifactId>
           <version>2.2.2</version>
               <!-- <exclusions>
                    <exclusion>
                      <groupId>org.slf4j</groupId>
                      <artifactId>slf4j-jdk14</artifactId>
                    </exclusion>
               </exclusions> -->
        </dependency>
        <dependency>
           <groupId>org.apache.tiles</groupId>
           <artifactId>tiles-jsp</artifactId>
           <version>2.2.2</version>
        </dependency>
        <dependency>
           <groupId>org.apache.tiles</groupId>
           <artifactId>tiles-servlet</artifactId>
           <version>2.2.2</version>
        </dependency>
        <dependency>
           <groupId>org.apache.tiles</groupId>
           <artifactId>tiles-template</artifactId>
           <version>2.2.2</version>
        </dependency>

        <!-- compile only, runtime container will provide this -->
        <!-- Need this for config annotation -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

    <!-- ceeliveDAO - Custom dependency -->
    <dependency>
         <groupId>com.cimple</groupId>
         <artifactId>ceeliveDAO</artifactId>
         <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>ceelive</finalName>

          <pluginManagement>
        <plugins>

            <!-- <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat6-maven-plugin</artifactId>
              <version>2.2</version>
            </plugin> -->
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
            </plugin>

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.3</version>
              <configuration>
                <source>1.8</source>
                <target>1.8</target> 
              </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.11.v20150529</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/spring4ajax</contextPath>
                    </webApp>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                    <wtpContextName>spring4ajax</wtpContextName>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>


            <plugin>
                <groupId>it.session.maven.plugins</groupId>
                <artifactId>tiles-maven-plugin</artifactId>
                <version>$maventiles.plugin.version</version>
                <extensions>true</extensions>
            </plugin>
         </plugins>
      </pluginManagement>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>service</display-name>

    <!-- Deafult pages -->
    <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Log listner -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>ceelive</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet/ceeliveservlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Servlet Mapping -->
   <servlet-mapping>
      <servlet-name>ceelive</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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.ceelive.controller" />

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $webappRoot/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Event Dao Bean -->
    <beans:bean id="eventDao" class="com.ceelive.dao.manager.event.EventDaoImpl">
    </beans:bean>
    <!-- Artist Dao Bean -->
    <beans:bean id="artistDao" class="com.ceelive.dao.manager.artist.ArtistDaoImpl">
    </beans:bean>
    <!-- Event Service Bean -->
    <beans:bean id="eventService" class="com.ceelive.service.event.EventServiceImpl">
        <beans:property name="eventDao" ref="eventDao"></beans:property>
    </beans:bean>
    <!-- Artist Service Bean -->
    <beans:bean id="artistService" class="com.ceelive.service.artist.ArtistServiceImpl">
        <beans:property name="artistDao" ref="artistDao"></beans:property>
    </beans:bean>
    <!-- Event Controller Bean -->
    <beans:bean id="eventController" class="com.ceelive.controller.EventController">
        <beans:property name="eventService" ref="eventService"></beans:property>
        <beans:property name="beanUtil" ref="beanUtil"></beans:property>
    </beans:bean>
    <!-- Artist Controller Bean -->
    <beans:bean id="artistController" class="com.ceelive.controller.ArtistController">
        <beans:property name="artistService" ref="artistService"></beans:property>
        <beans:property name="beanUtil" ref="beanUtil"></beans:property>
    </beans:bean>
    <!-- BeanUtil bean -->
    <beans:bean id="beanUtil" class="com.ceelive.utility.BeanUtil">
    <!-- HibernateConfiguration bean -->
    </beans:bean>
       <beans:bean id="hibernateConfiguration" class="com.ceelive.configuration.HibernateConfiguration">
    </beans:bean>
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
        <beans:bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <beans:property name="definitions">
            <beans:list>
                <beans:value>/WEB-INF/tiles.xml</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean>
    <beans:bean id="tilesViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <beans:property name="viewClass">
            <beans:value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </beans:value>
        </beans:property>
        <beans:property name="order" value="0"/>
    </beans:bean>

    <context:component-scan base-package="com.frameworkonly.tilesapp" />
</beans:beans>

【问题讨论】:

你能检查一下,你的ArtistDao在包裹com.ceelive.dao.manager.artist中吗? 非常感谢您的及时回复。但是,是的,该文件位于正确的路径。正如我所提到的,该项目在我的系统上运行良好;但不在其他人身上。 DAO layer or database layer is created as separate project and included into project- 似乎 DAO 层 jar 在编译时存在,但在运行时找不到。检查您的运行时类路径与其他路径之间的差异。 虽然你的回答现在听起来更有效。我能应付。如果这是主要原因,我会很明确地告诉你。但是感谢您的支持。 【参考方案1】:

我通过在 Eclipse 中将 Tomcat 服务器添加到 Java 构建路径中解决了同样的错误。

右键项目->构建路径->配置构建路径->添加库..(出现在对话框右侧)->服务器运行时->选择“Apache Tomcat v..”->完成

然后在服务器上运行项目。

【讨论】:

【参考方案2】:

我认为是版本冲突导致的异常。请检查java和tomcat服务器的版本。

【讨论】:

感谢您的回复兄弟。该项目建立在 Java 8 和 tomcat 7 之上。正如所说,它在创建它的机器上运行良好。但它没有在其他机器上运行。虽然在那里观察到相同的技术。 好的...首先你应该从你的机器创建war文件并部署在其他机器服务器上。如果应用程序工作正常而不是在清理并安装应用程序之后从其他开发人员机器中删除 .m2 文件。它应该工作。如果您使用的是 eclipse,请转到项目的属性并匹配项目方面。 感谢 Akash 的提示。虽然当我在其他机器上复制我的 .m2 时它起作用了:应用程序运行良好。但我必须找到缺失的依赖项。

以上是关于错误 org.springframework.web.servlet.DispatcherServlet - 上下文初始化失败的主要内容,如果未能解决你的问题,请参考以下文章

springmvc简单使用

Spring IOC-ContextLoaderListener

web.xml配置遇到的问题

解决springmvc返回json中文乱码

配置web.xml中Filter的(字符)过滤器

Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL