public class BasicDAOImpl extends HibernateDaoSupport implements BasicDAO{
public List findByHql(String hql)throws DataAccessException{
System.out.println("1111111111111"+getHibernateTemplate());
List list = new ArrayList();
list = getHibernateTemplate().find(hql);
return list;
}
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
BasicDAO dao= (BasicDAO )context.getBean("basicDAO");
BasicDAO dao= new BasicDAO();
List list = dao.findByHql("");
System.out.println("ffffffffffffffff"+list.size());
}
}
说明:如果红色部分注释掉改用绿色部分的话,则getHibernateTemplate()为空,原因:
(1)HibernateDaoSupport 需要注入SessionFactory对象
(2)new 一个新对象,并没有注入SessionFactory对象,因此你应该从spring容器中去获取对象