【问题标题】:Issue in executing Spring Web project in Eclipse on Tomcat server在 Tomcat 服务器上的 Eclipse 中执行 Spring Web 项目的问题
【发布时间】:2011-01-25 20:19:58
【问题描述】:

我有一个使用 Maven 编译/构建的 Spring Web 项目。构建项目没有问题。我正在尝试在 Tomcat (v6) 服务器上的 Eclipse (3.3.2) 中运行该项目。

作为 Spring 项目的一部分,我在 WEB-INF 目录中有一个 spring-servlet.xml 文件。此文件包含另一个具有数据源配置的资源 xml 文件。

<import resource="classpath:${datasourceInclude}.xml"/>

现在,当使用 Maven 编译项目时,它会解析变量 ${datasourceInclude} 并将其设置为适当的值,从而生成具有适当值的 spring-servlet.xml。

<import resource="classpath:datasourceLocal.xml"/>

但是当我尝试在 Eclipse (Tomcat) 中运行该项目时,我收到以下错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:${datasourceInclude}.xml]
Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [${datasourceInclude}.xml]; nested exception is java.io.FileNotFoundException: class path resource [${datasourceInclude}.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
...
...

基本上,当我启动 Tomcat 时,它会尝试从 /src/main/webapp/WEB-INF 文件夹中选择 spring-servlet.xml,该文件夹具有 ${datasourceInclude} 变量。

谁能告诉我如何解决这个问题,这样我就不必更改 spring-servlet.xml 并添加硬代码值来代替 ${datasourceInclude} 变量。

【问题讨论】:

    标签: eclipse tomcat maven-2 spring-mvc m2eclipse


    【解决方案1】:

    尝试将war:inplace添加到Maven项目配置页面中为资源过滤执行的目标列表中。

    右键单击您的项目,然后转到 Properties > Maven > Lifecycle Mapping 并将 war:inplace 添加到 Goals 以调用资源更改如下图:

    【讨论】:

    • 我有同样的问题,STS 2.7,m2eclipse,在这种情况下,tomcat 7 现在在内部使用,还有其他想法为什么不调用它或如何追踪问题?
    • 升级到 STS 2.8,有了新的 m2e 支持,过滤效果很好,开箱即用-
    【解决方案2】:

    谁负责解析 Spring XML 中的属性/变量名称?这是在编译时通过 Maven 完成的,还是应该在运行时发生?如果在运行时,您是否使用 Spring 的 PropertyPlaceholderConfigurer

    【讨论】:

    • 这是由 Maven 在编译时完成的。我指定了一些 Maven 配置文件来加载具有变量值的资源包文件。
    • 那么当您从 Eclipse 运行时,您希望它如何工作?您是否在 Eclipse 中使用 Maven 插件,例如 M2E?
    • 是的,我正在为 Eclipse 使用 Maven 插件 M2E。我的问题是如何配置 Eclipse 以更改 spring-servlet.xml 文件中的变量,然后再将其加载到 Tomcat 中。
    猜你喜欢
    • 2014-10-07
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多