首页 文章

Spring注入的DataSource为null

提问于
浏览
1

我正在尝试使用JdbcTemplate创建DAO . 但似乎 Spring 季注射不顺利 . 我正在从Tomcat注入带有JNDI的DataSource .

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

我错过了什么,为什么 Spring 天不注射它?

道的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 log http://shorttext.com/700e4579 tomcat catalina log http://shorttext.com/700f24cb

1 回答

  • 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

    edit:

    我再次阅读你的评论,如果

    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层,但现在这太复杂了......

相关问题