【问题标题】:Spring app doesn't find datasource in config filesSpring 应用程序在配置文件中找不到数据源
【发布时间】:2012-05-18 20:09:49
【问题描述】:

当我将我的 spring 应用程序 (Spring 2.5.6) 部署到 Tomcat (6.0) 上时,启动失败并显示

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [C.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

我的 web.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="[snip]" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>[snip]</display-name>
<welcome-file-list>
   [snip]
</welcome-file-list>

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

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

<servlet>
    <servlet-name>[snip]</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>[snip]</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

All.xml 导入多个配置文件:

<import resource="A.xml"/>
<import resource="B.xml"/>
<import resource="C.xml"/>
...

B.xml 定义了一个数据源:

<jee:jndi-lookup id="dataSource" jndi-name="[snip]"/>

C.xml 创建一个休眠会话工厂,引用来自 B.xml 的数据源:

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingResources">
        <list>
            <value>[snip]</value>
            <value>[snip]</value>
        </list>
    </property>
    <property name="hibernateProperties">
  <props>
       [snip]
  </props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>

当我部署我的 WAR 文件时,会发生上述异常。我的问题是:

  1. 为什么?
  2. 为什么在我编写的用于测试 spring 配置有效性的 JUnit 测试中没有出现相同的异常?这是测试:

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/All.xml" }) 公共类 ImfSpringConfigurationTest { [剪辑]] }

注意:在 JUnit 测试中,我使用不同的 All.xml 文件将 B.xml 替换为定义数据源的文件,而无需 jndi 查找,如下所示:

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName = "oracle.jdbc.driver.OracleDriver"
    [snip] />

**

事实证明,这个配置是正确的。是另一个模块的部署在一些相同的配置文件上运行引发了异常,而不是这个部署过程。

【问题讨论】:

    标签: spring configuration datasource jndi


    【解决方案1】:

    尝试使用通配符拉入顶级上下文:

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/classes/spring*.xml</param-value>
    </context-param>
    

    【讨论】:

    • 抱歉,这假设您的上下文不称为 A B C 等,并且共享一些公共前缀。我知道我在导入层次结构方面遇到了问题,就像你以前一样。
    • 对不起伙计,误报。异常来自另一个战争文件部署。还是谢谢。
    猜你喜欢
    • 2016-10-22
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多