Spring JNDI datasource - no qualifying bean of type [javax.sql.DataSource] found

限于喜欢 提交于 2019-12-24 05:03:48

问题


I've defined a JDNI resource in tomcat 7, but Spring is failing to inject it.

Here's my 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>

This is the relevant portion of my 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>

Here's my entire 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>

And this is the @Autowired bean:

@Autowired
private DataSource dataSource;

For some reason, I get this in the tomcat log:

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

I know my Tomcat JNDI setup is correct because if I comment out the autowired bean and just do this...

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

...then the dataSource works as expected.

Any ideas what I'm doing wrong?

来源:https://stackoverflow.com/questions/21826538/spring-jndi-datasource-no-qualifying-bean-of-type-javax-sql-datasource-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!