ssh整合getHibernateTemplate().find()报空指针异常

[color=darkred][/color]
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
        value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
    </property>
    <property name="url"
        value="jdbc:microsoft:sqlserver://localhost:1433;databasename=zf">
    </property>
    <property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.SQLServerDialect
            </prop>
            <prop key="hibernate.show_sql">
                true
            </prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/chen/model/TblFwlx.hbm.xml</value>
            <value>com/chen/model/TblFwxx.hbm.xml</value>
            <value>com/chen/model/TblJd.hbm.xml</value>
            <value>com/chen/model/TblQx.hbm.xml</value>
            <value>com/chen/model/TblUser.hbm.xml</value></list>
    </property></bean>

                   <bean name="FwxxDao" class="com.cao.dao.FwxxDao"/>
                <bean name="LoginDao" class="com.cao.dao.LoginDao"/>

applicationcontext-aop.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">

<aop:config proxy-target-class="true">
    <aop:advisor
        pointcut="execution(* com.cao.dao.*Dao.*(..))"
        advice-ref="txAdvice" />
</aop:config>


<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*"  propagation="REQUIRED" read-only="true"/>
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="*"  propagation="REQUIRED" read-only="true"/>
    </tx:attributes>
</tx:advice>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

FwxxAction代码:

public class FwxxAction extends DispatchAction {
private FwxxDao fwxxDao;

public FwxxDao getFwxxDao() {
return fwxxDao;
}
public void setFwxxDao(FwxxDao fwxxDao) {
this.fwxxDao = fwxxDao;
}
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return doList(mapping, form, request, response);
}
public ActionForward doList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("in..........doList");
System.out.println("fwxxDao======="+fwxxDao);
int index=1;
int pageSize=5;
try{
String str=request.getParameter("index");
index=Integer.parseInt(str);

    }catch(Exception e){

    }
    List list=fwxxDao.getAll();
    request.setAttribute("list", list);
    return mapping.findForward("fwxxList");

}

}

Dao代码:

package com.cao.dao;

import java.util.List;

import com.cao.model.tblFwxx;

public class FwxxDao extends BaseDao {

public FwxxDao() {
    super.cls=tblFwxx.class;
}

public List getFwxx(){
    return super.getAll();  
}
}

BaseDao代码:

public class BaseDao extends HibernateDaoSupport {

protected Class cls;

public List<T> getAll(){

    return getHibernateTemplate().findByExample(cls);
}

}

struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">







attribute="fwxxForm"
name="fwxxForm"
parameter="method"
path="/fwxx"
scope="request"
type="com.cao.struts.action.FwxxAction" >





action-servlet.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName">

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>


contextConfigLocation
classpath:applicationContext-*.xml

<!-- 著名 Character Encoding filter -->

encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8

<!--Hibernate Open Session in View Filter控制Session的连接-->
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>


org.springframework.web.context.ContextLoaderListener


action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


debug
3


detail
3

0


action
*.do


index.jsp

最后运行,getHibernateTemplate()为空,调用它的find方法抛空指针异常

用ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext-*.xml");
con.getBean("FwxxDao");
可以得到对象并可以调它的方法

[code="java"]public void setFwxxDao(FwxxDao fwxxDao) {
this.fwxxDao = fwxxDao;
} [/code]
[code="java"]default-autowire="byName"[/code]
byName的name是java中setXxxx 的Xxxx。
举个例子:
[code="java"]public class Service
{
Source source1;

public void setSource(Source source1)
{
    this.source1 = source1;
}

}[/code]
applicationContext.xml为
[code="xml"]<beans

...

default-autowire="byName">

<bean id="source" class="cn.hh.spring.DBCPSource" scope="prototype"/>
   </bean>


[/code]

修改 applicationContext.xml文件中的fwxxDao bean
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

class="org.apache.commons.dbcp.BasicDataSource">
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

value="jdbc:microsoft:sqlserver://localhost:1433;databasename=zf">



class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">






org.hibernate.dialect.SQLServerDialect


true





com/chen/model/TblFwlx.hbm.xml
com/chen/model/TblFwxx.hbm.xml
com/chen/model/TblJd.hbm.xml
com/chen/model/TblQx.hbm.xml
com/chen/model/TblUser.hbm.xml


//fwxxDao首字母小写
//添加sessionFactory

//loginDao小写
//添加...

修改 applicationContext.xml文件中的fwxxDao bean
[code="xml"]<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

class="org.apache.commons.dbcp.BasicDataSource">
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

value="jdbc:microsoft:sqlserver://localhost:1433;databasename=zf">



class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">






org.hibernate.dialect.SQLServerDialect


true





com/chen/model/TblFwlx.hbm.xml
com/chen/model/TblFwxx.hbm.xml
com/chen/model/TblJd.hbm.xml
com/chen/model/TblQx.hbm.xml
com/chen/model/TblUser.hbm.xml


//fwxxDao首字母小写
//添加sessionFactory

//loginDao小写
//添加...
[/code]