Spring注入的DataSource为null

时间:2015-12-29 22:48:18

标签: java spring tomcat datasource jndi

我正在尝试使用JdbcTemplate创建DAO。但似乎春季注射不顺利。我正在使用Tomcat的JNDI注入DataSource。

我还在Tomcats server.xml,/ META-INF中的ResourceLink,web.xml中的resource-ref中编写了设置,尝试在web.xml中添加上下文侦听器,它也没有帮助(实际上,我应该添加监听器,如果我不是从servlet访问DataSource,而只是从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>

的web.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 localhost日志 http://shorttext.com/700e4579 tomcat catalina日志 http://shorttext.com/700f24cb

1 个答案:

答案 0 :(得分:0)

与我的工作示例相比,我只能看到差异,我在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
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层访问,但现在这太复杂了......