java - Spring注入(inject)的DataSource为空

标签 java spring tomcat datasource jndi

我正在尝试使用 JdbcTemplate 制作 DAO。但似乎 Spring 注入(inject)并不顺利。我正在使用来自 Tomcat 的 JNDI 注入(inject)数据源。

另外我在Tomcats server.xml中写了设置,在/META-INF中写了ResourceLink,在web.xml中写了resource-ref,试图在web.xml中添加context-listener,它也没有帮助(实际上,我应该添加监听器,如果我不是从 servlet 访问数据源,而是仅从 DAO 访问?)。

我错过了什么,为什么 Spring 不注入(inject)它?

dao-context.xml

 <context:component-scan base-package="somepackage"/>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/phonebook" expected-type="javax.sql.DataSource"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>

网络.xml

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

META-INF/context.xml

<Context>
<ResourceLink name="jdbc/phonebook" global="jdbc/global_phonebook" type="javax.sql.DataSource"/>

Tomcat 中的 server.xml

<Resource name="jdbc/global_phonebook" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/phonebook" username="root" password="1234" maxActive="10" maxIdle="5" maxWait="-1" defaultAutoCommit="false" defaultTransactionIsolation="READ_COMMITTED"/>

tomcat 本地主机日志 http://shorttext.com/700e4579 tomcat catalina日志 http://shorttext.com/700f24cb

最佳答案

与我的工作示例相比,我能看到的唯一区别是,我在 Tomcat 的 context.xml 中有这个:

<Resource name="jdbc/MySQL"
            auth="Container"
            type="javax.sql.DataSource"
            username="root"
            password=""
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/so"
            maxActive="8"
            maxIdle="4"/>

我使用 Tomcat 7.0.47 对此进行了测试,并遵循了此文档 - https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

编辑:

我又看了一遍你的评论,如果

jdbcTemplate.getDataSource()

抛出 NPE,则 jdbcTemplate 必须为 null。你能分享你的 DAO 代码吗?


嗨,Misha,很抱歉回答晚了。

我创建了非常简单的 Controller 来测试:

@Controller
public class ExecuteController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @ResponseBody
    @RequestMapping(path="execute")
    public String execute() {
        return new Date().toString() + ": executed " + (jdbcTemplate == null);
    }

}

当我执行它时,我得到了

Wed Jan 06 09:22:59 CET 2016: executed false

false 表示它不为空。

我有空的根上下文,servlet 上下文是:

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

    <context:component-scan base-package="test" />

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/MySQL" expected-type="javax.sql.DataSource"/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>

</beans>

因此,component-scan@Controller@Autowired 的组合可以达到目的。

在现实生活中我不会从 Controller 访问 JdbcTemplate,而是通过服务和 dao 层,但现在这太复杂了......

关于java - Spring注入(inject)的DataSource为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34520319/

相关文章:

java postgresql-没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换

java - 机器人一遍又一遍地打印相同的字符串

java - 使用什么数据结构(在 JAVA 中)与随机顺序的 3 组键

mysql - 在 spring 中,如何在一次迭代发生事务回滚后继续来自 Controller for 循环的请求

spring - 如何在 Spring MVC 和 Tomcat 中配置映射

java - 文件上传流从哪里获取内容?

java - DeferredResult 能否在 Spring MVC 4 中与 @ModelAttribute 一起使用?

java - Spring Security 在休息服务中获取用户信息,用于经过身份验证和未经过身份验证的用户

java - JPA REQUIRES_NEW 事务到底什么时候提交

java - JSP/TomCat 中不显示 img