1

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

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

我有 2 个文件:

  • applicationContext-web.xml 在 WEB-INF 旁边的 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

4

2 回答 2

3

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

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

此外,在根目录中使用模式也存在一些问题。

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

于 2011-12-06T14:51:48.350 回答
1

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

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>
于 2011-12-06T15:39:01.957 回答