Spring里的bean配置如下:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/ssh"></property>
<property name="username" value="root"></property>
<property name="password" value="fxfly"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/Sshdemo.hbm.xml</value>
</list>
</property>
</bean>
<bean id="testDao" class="com.test.TestDaoImp">  
<property name="sessionFactory" ref="sessionFactory" />
</bean>
DaoIMP如下:
public class TestDaoImp extends HibernateDaoSupport implements TestDao {
public boolean checkUser(String user, String pass) {
// TODO Auto-generated method stub
String hql = "from Sshdemo s where s.SName='" + user
+ "' and s.SPass='" + pass + "'";
List lt = null;
try {
System.out.println(this.getHibernateTemplate());
lt = this.getHibernateTemplate().find(hql);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(lt);
if(lt.size()>0){
return true;
}
return false;
}
}
为什么这个this.getHibernateTemplate()=null;

解决方案 »

  1.   

    <bean id="testDao" class="com.test.TestDaoImp">   
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>看你的配置有注入进来,应该是spring配置懒加载的问题,当你的使用的时候才加载对象,该例中执行find方法的时候才会注入sessionFactory对象
      

  2.   

    那我用的时候是不是还得在class里getter TestDaoImp这个对象,谢谢
      

  3.   

    如果 sessionFactory 初始话正常的不应该空才对..你重写一下 TestDaoImp 的 setSessionFactory(SessionFactory)方法 输出一个字符串 看看能否进行正常注入