Spring JNDI数据源 - 找不到类型[javax.sql.DataSource]的限定bean

时间:2014-02-17 10:29:45

标签: tomcat spring-mvc jndi

我在tomcat 7中定义了一个JDNI资源,但是Spring没有注入它。

这是我的META-INF / context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/TyedArtDB"
              auth="Container"
              driverClassName="org.postgresql.Driver"
              factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
              maxActive="10"
              maxIdle="2"
              type="javax.sql.DataSource"
              url="jdbc:postgresql://localhost:5432/tyedart"
              username="user"
              password="pass"/>
</Context>

这是我的web.xml的相关部分:

<resource-ref>
    <description>DB</description>
    <res-ref-name>jdbc/TyedArtDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

这是我的整个servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/TyedArtDB"/>

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <context:component-scan base-package="com.tyedart.web" />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

</beans:beans>

这是@Autowired bean:

@Autowired
private DataSource dataSource;

出于某种原因,我在tomcat日志中得到了这个:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency

我知道我的Tomcat JNDI设置是正确的,因为如果我注释掉自动装配的bean并且只是这样做......

InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/TyedArtDB");

...然后dataSource按预期工作。

任何想法我做错了什么?

0 个答案:

没有答案