java.lang.IllegalStateException:未找到 WebApplicationContext:未注册 ContextLoaderListener 或 DispatcherServ

Posted

技术标签:

【中文标题】java.lang.IllegalStateException:未找到 WebApplicationContext:未注册 ContextLoaderListener 或 DispatcherServlet 错误【英文标题】:java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered error 【发布时间】:2020-03-31 22:58:06 【问题描述】:

我在这个项目中使用 Spring、Gradle 和 Tomcatrunwar,但我一直面临这个错误。我在网上尝试了不同的解决方案,但没有任何帮助。我最近才加入这个项目,我对 serverlets 还比较陌生 我的 web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="fineract-platform-provider" version="3.0">


    <display-name>Fineract Platform</display-name>
    <filter>
        <filter-name>filterChainProxy</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter>
        <filter-name>filterChainProxy2</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter>
        <filter-name>filterChainProxy3</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filterChainProxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>filterChainProxy2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>filterChainProxy3</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <description>A provider of microfinance services which applications can
        consume once authenticated and authorized.</description>

    <!--  =========================================================================
            web.xml is no longer used since the switch to Spring Boot.

            org.apache.fineract.infrastructure.core.boot.war.WebApplicationInitializerConfiguration
            and org.apache.fineract.infrastructure.core.boot.WebXmlConfiguration take its place now.
          =========================================================================
      -->
    <absolute-ordering/>
</web-app>```

I don't have a applicationContext.xml in my project, the IDE creates the spring application context and I can't access the file. So adding a listener for the ContextLoader points that out in a new error,
What can I do, please advice

【问题讨论】:

【参考方案1】:

我添加了侦听器并从 /META-INF/Spring 文件夹中的现有 appContext.xml 创建了 applicationContext.xml,但是当我尝试 gradlew tomCatRunWar 任务时会产生一堆新错误 applicationContext.xml

<!--

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership. The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied. See the License for the
    specific language governing permissions and limitations
    under the License.

-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:sec="http://www.springframework.org/schema/security"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <sec:global-method-security
            pre-post-annotations="enabled" />

    <tx:annotation-driven />

    <context:component-scan
            base-package="org.apache.fineract.accounting.*,
                                          org.apache.fineract.commands.provider,
                                          org.apache.fineract.commands.handler,
                                          org.apache.fineract.commands.service,
                                          org.apache.fineract.commands.*,
                                          org.apache.fineract.accounting.*,
                                          org.apache.fineract.infrastructure.*,
                                          org.apache.fineract.scheduledjobs.*,
                                          org.apache.fineract.organisation.*,
                                          org.apache.fineract.portfolio.loanaccount.*,
                                          org.apache.fineract.portfolio.savings.*,
                                          org.apache.fineract.portfolio.*,
                                          org.apache.fineract.useradministration.*,
                                          org.apache.fineract.mix.*,
                                          org.apache.fineract.template.*,
                                          org.apache.fineract.template.service,
                                          org.apache.fineract.batch">
        <context:exclude-filter expression="org.springframework.stereotype.Controller"
                                type="annotation" />

        <!-- We do NOT want all @Configuration "beans" to be auto-detected by ComponentScan,
             but we want to use / import them explicitly in Tests & Spring Boot applications,
             or other import in other @Configuration, so that we could have mutually exclusive ones.
         -->
        <context:exclude-filter expression="org.springframework.context.annotation.Configuration"
                                type="annotation" />
    </context:component-scan>

    <bean id="auditorAware"
          class="org.apache.fineract.infrastructure.core.domain.AuditorAwareImpl" />
    <jpa:auditing auditor-aware-ref="auditorAware" />

    <jpa:repositories base-package="org.apache.fineract.commands.domain" />
    <jpa:repositories base-package="org.apache.fineract.infrastructure.*.domain" />
    <jpa:repositories base-package="org.apache.fineract.accounting.*.domain" />
    <jpa:repositories base-package="org.apache.fineract.useradministration.domain" />
    <jpa:repositories base-package="org.apache.fineract.organisation.*.domain" />
    <jpa:repositories base-package="org.apache.fineract.portfolio.*" />
    <jpa:repositories base-package="org.apache.fineract.mix.domain" />
    <jpa:repositories base-package="org.apache.fineract.scheduledjobs.*" />
    <jpa:repositories base-package="org.apache.fineract.template.domain" />

    <import resource="infrastructure.xml" />

    <import resource="securityContext.xml" />

    <import resource="cache.xml" />

    <bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster">
        <property name="taskExecutor">
            <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
        </property>
    </bean>

    <import resource="spmContext.xml"/>
</beans>

Web.xml



<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="fineract-platform-provider" version="3.0">

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <context-param>
        <param-name>appContext</param-name>
        <param-value>/META-INF/spring/appContext.xml</param-value>
    </context-param>
   <!--
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
-->


    <display-name>Fineract Platform</display-name>
    <filter>
        <filter-name>filterChainProxy</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter>
        <filter-name>filterChainProxy2</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter>
        <filter-name>filterChainProxy3</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filterChainProxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>filterChainProxy2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>filterChainProxy3</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <description>A provider of microfinance services which applications can
        consume once authenticated and authorized.</description>


    <!--  =========================================================================
            web.xml is no longer used since the switch to Spring Boot.

            org.apache.fineract.infrastructure.core.boot.war.WebApplicationInitializerConfiguration
            and org.apache.fineract.infrastructure.core.boot.WebXmlConfiguration take its place now.
          =========================================================================
      -->


    <absolute-ordering/>
</web-app>```


Link to log file https://docs.google.com/document/d/1OD5XpoJ5JMTbeGMhYelvZWrulIAXe-dDOpGJPsfv-nc/edit?usp=sharing

【讨论】:

以上是关于java.lang.IllegalStateException:未找到 WebApplicationContext:未注册 ContextLoaderListener 或 DispatcherServ的主要内容,如果未能解决你的问题,请参考以下文章