日期:2014-05-17  浏览次数:20761 次

ibatis+spring 注入的DAO总是空指针
这是applicationContextv.xml
XML code

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver
            </value>
        </property>
        <property name="url">
            <value> jdbc:mysql://localhost:3306/test
            </value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>
    
    <!-- 集成ibatis -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="WEB-INF/sqlmap-config.xml" />
         <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="personDao" class="com.gogoosoft.dao.impl.PersonDaoImp">
        <property name="sqlMapClient" ref="sqlMapClient"></property>
    </bean>
    <bean id="loginAction" class="com.gogoosoft.action.LoginAction">
        <property name="personDao" ref="personDao"></property>
    </bean>
</beans>





在loginAction里调用personDao,总是空指针,求解,捣鼓一天了

------解决方案--------------------
personDao在spring那里注入了没
------解决方案--------------------
额,不会啊,dao、action都有注入了啊,你的personDao在action中有set\get()方法吗,别忘记它了
------解决方案--------------------
personDao在action里面是要有相关的get和set方法的。还有,那个personDao是要和Spring的配置文件里面一样的。
------解决方案--------------------
就是这个applicationContext.xml。。
------解决方案--------------------
对了,,你这个是struts1,要在struts-config.xml里面配置一个control或者是plug-in


plug-in是这么配的,不过要加一个plug-in的插件包的,包名叫“spring-webmvc-struts.jar”

XML code

<plug-in

        className="org.springframework.web.struts.ContextLoaderPlugIn">

        <set-property property="contextConfigLocation"

            value="/WEB-INF/applicationContext.xml" />