【问题标题】:Spring config not found找不到弹簧配置
【发布时间】:2012-01-14 03:56:39
【问题描述】:

我的 web.xml 中有以下内容:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
</context-param>

我有 2 个文件:

  • web.xml旁边WEB-INF中的applicationContext-web.xml
  • myapp-service.jar 中的applicationContext-service.xml

部署应用程序时,我得到一个

没有为依赖找到类型为 [AServiceBean] 的匹配 bean:预期 至少 1 个 bean 有资格作为自动装配候选者 依赖。

似乎找不到 applicationContext-service.xml。如果我将它复制到 web.xml 旁边,它可以正常工作。我不明白为什么会这样。

服务器是 Tomcat 6。

感谢任何帮助。 谢谢。

编辑

澄清:如果我使用

<param-value>
    classpath:applicationContext-web.xml,
    classpath:applicationContext-service.xml
</param-value>

应用部署没有任何问题,因此只需找到(或不找到)applicationContext-service.xml

【问题讨论】:

  • 你配置了component-scan 吗?

标签: spring configuration applicationcontext


【解决方案1】:

尝试使用classpath*:applicationContext-*.xml(冒号前有星号)。

但它可能不起作用,例如JBoss 有问题,要让它工作,你需要使用一些来自 jboss 的特殊类加载器。

另外,根目录中有一些problems using patterns

无论如何,我建议避免使用模式,最好使用两个明确的 import 语句来创建 applicationContext.xml

【讨论】:

  • 不。类路径 *... 不起作用。也在Tomcat 7上试过。没有运气。
  • 谢谢。这也是我得出的结论(避免使用通配符)。我会研究一下导入的想法 - 以前从未使用过,也许是时候尝试一下了。
【解决方案2】:

您需要将配置文件放入类路径中。

WEB-INF/classess  is the directory you need to place your configuration files
classpath:applicationContext-*.xml will then work

或类似的东西将它们放在一个地方

WEB-INF/classes/spring   
classpath:spring/applicationContext-*.xml

applicationContext-service.xml : 如果jar文件里已经有这个就不需要复制了


示例 main-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-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/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd         
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<import resource="classpath:spring/config1.xml" />
<import resource="classpath:spring/config2.xml" />
.
.
<import resource="classpath:spring/configN.xml" />


</beans>

【讨论】:

  • 你没有听懂我的问题。配置文件位于类路径中。而没有找到的正是 -service 之一,它在一个 JAR 中。
  • 是的,我的错。服务 xml 在您的 jar 文件中在哪里?它是在jar的根目录还是在某个目录中?
  • 它在 JAR 的根目录中。我在 Spring 文档中看到通配符可能存在问题。最后,我可能不得不坚持使用上下文文件名列表而不是使用通配符,特别是因为这个应用程序必须能够在许多服务器和版本上运行。
  • 您可以将所有配置文件一并导入并在 web.xml 中传递
  • 见上文,我已经更新了一些导入其他配置文件的示例文件。
猜你喜欢
  • 1970-01-01
  • 2011-09-11
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多