日期:2014-05-19  浏览次数:20773 次

SSH程序service层调用dao层出空指针异常
我直接通过页面调用程序不会有问题

现在是通过监听直接执行service层的方法,不经过struts的Action

直接在service层调用dao层的方法
发现得到空指针异常
public class SearchEngerneServiceImpl implements SearchEngerneService {

private static ISearchEngerneDao isearchengernedao;

public void timeTask(){
List l = this.findAll();
System.out.println("l.size="+l.size());
}//是被调用的方法
public List findAll() {
return isearchengernedao.findAll();
}

public ISearchEngerneDao getIsearchengernedao() {
return isearchengernedao;
}

public void setIsearchengernedao(ISearchEngerneDao isearchengernedao) {
this.isearchengernedao = isearchengernedao;
}
 }


经过检查 private static ISearchEngerneDao isearchengernedao;中的

isearchengernedao==null也就是没有得到这个对象
但是明明没有错啊
我通过页面直接跑的时候没有问题isearchengernedao也不会为空
这里不知道为何

这里是dao层的代码,接口就不贴出来了
public class SearchEngerneImpl extends HibernateDaoSupport implements
ISearchEngerneDao {
public List findAll() throws HibernateException {

return this.getHibernateTemplate().executeFind(new HibernateCallback() {

public List doInHibernate(Session session)
throws HibernateException, SQLException {
Transaction tx = session.beginTransaction();
Query query = session
.createQuery(" FROM TDoc AS t");
tx.commit();
return query.list();
}
});
}
}


接下来是配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans
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="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@192.168.2.212:1521:orcl">
</property>
<property name="username" value="strongbi"></property>
<property name="password" value="dss"></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.Oracle9Dialect
</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/strongit/po/TDoc.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessi