spring - 为 Tomcat 构建的 Maven WAR : "Could not open ServletContext resource [/WEB-INF/applicationContext.xml]"

标签 spring maven tomcat docker

关于这个主题已经有一些问题,但我的 Tomcat 仍然运行不流畅。
我的工作区包括 jsf 和 spring。只要我在 Eclipse 中本地构建和运行项目并将其部署到我在 Eclipse 中的 Tomcat,一切都运行良好。但是现在我想将 war 文件部署到外部 Tomcat 中,在我的例子中是 Docker-Container 中的 Tomcat。

所以我要做的是:
* 使用以下 Maven-pom(提取)构建 WAR 文件。
* 并像这样将 war 文件复制到我正在运行的容器中:

docker cp projectName.war containerID:/usr/local/tomcat/webapps/

在 Tomcat 启动期间,我总是收到以下错误。

一开始我直接在 WEB-INF 文件夹中有 applicationContext.xml 文件。但我读到将它放在“\WEB-INF\classes”中会更好,所以我将它移到那里,并在 web.xml 中执行了以下上下文位置参数。

因此工作区层次结构如下图所示 项目名称
| -- 网络内容
| -- 网络信息
| -- pom.xml

另一件事是,当我在构建后提取 war 文件以查看 applicationContext.xml-File 的实际位置时,我在“\WEB-INF\classes”中找到了它,但是上下文位置参数在 web.xml 中找不到它。我还尝试将 applicationContext.xml-File 的副本放入 src/main/resources,这导致该文件位于 WAR 文件中的正确位置,但 web. xml 已更改。

有人建议将 applicationContext.xml 文件放在哪里,以及如何在构建过程中以正确的方式包含和指向它。还有为什么 web.xml 在构建过程中会发生变化。还是存在完全不同的问题?

感谢您的帮助。

pom.xml

<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <jsf.version>2.2.14</jsf.version>
    <spring.version>4.3.6.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>${jsf.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>${jsf.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <!-- JSF Ende -->

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.1.0.jre8</version>
    </dependency>

    <!-- Spring Start -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- Spring Ende -->
</dependencies>

<build>
    <finalName>projectName</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <!--<webXml>WebContent\WEB-INF\web.xml</webXml>-->
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

启动错误

2017-02-24 03:15:44 ERROR ContextLoader:351 - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 25 more
24-Feb-2017 03:15:44.613 SEVERE [localhost-startStop-2] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
24-Feb-2017 03:15:44.614 SEVERE [localhost-startStop-2] org.apache.catalina.core.StandardContext.startInternal Context [/projectName] startup failed due to previous errors
2017-02-24 03:15:44 INFO  XmlWebApplicationContext:987 - Closing Root WebApplicationContext: startup date [Fri Feb 24 03:15:44 UTC 2017]; root of context hierarchy
2017-02-24 03:15:44 WARN  XmlWebApplicationContext:1005 - Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Fri Feb 24 03:15:44 UTC 2017]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:417)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1002)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:961)
at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:581)
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4900)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5537)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:149)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
24-Feb-2017 03:15:44.622 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive 

web.xml

  <!-- Add Support for Spring -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

Workspace-Hierarchy

最佳答案

现在过了一会儿,我做到了。

  1. “maven clean”,因为一些依赖项变得陈旧且无效。这就是为什么 web.xml 在构建期间不再更改的原因
  2. 需要 JSF 依赖项,因此不再“提供”
  3. Docker 容器有 JDK 7,但我的应用程序是用 JDK 8 构建的,所以我将容器更改为带有 JDK 8 的 Tomcat 8
  4. 需要它来告诉 Maven-Build web.xml 文件在哪里以及哪些文件应该包含在 WAR-Build 中。我使用以下 Maven 插件完成了此操作:(也不要忘记将 Maven 依赖项添加到“Eclipse 中的部署程序集”

ma​​ven-war-plugin

<plugin>            
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
   <warSourceDirectory>WebContent</warSourceDirectory>
   <webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

玩得开心

关于spring - 为 Tomcat 构建的 Maven WAR : "Could not open ServletContext resource [/WEB-INF/applicationContext.xml]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42444703/

相关文章:

java - Spring 获取 ServletContext 并将其作为 Bean 提供

maven - 为什么要为Jenkins使用SonarQube插件,而不是简单地使用Maven目标 Sonar : Sonar ?

java.lang.ClassNotFoundException : org. slf4j.impl.StaticLoggerBinder 我什至没有登录我的依赖项

java - 找不到适合 jdbc :oracle:thin:@localhost:1521:XE 的驱动程序

java - 在 Tomcat 中设置 Hibernate session 工厂

jakarta-ee - ActiveMQ 抛出 NullPointerException

java - Jpa注释的自定义用户类型错误属性映射的列数错误

java - Spring Integration http 出站网关 POST 响应不包含转义的符号

java - Spring Datasource事务管理器问题

java - 使用 Tycho 构建片段时出错 : "xyz.fragment cannot be installed in this environment because its filter is not applicable"