Spring注入SessionFactory的空指针异常

自己的错误在于,new的Dao实例,进行调用,以至于出现Dao中注入的Sessionfactory空指针。

在hibernate+spring配置过程中,应注意几点:

  1. 不能直接new一个DAO的实例,而应该通过spring的配置文件自动初始化session bean,然后再通过spring得到该DAO的引用,否则在调用getHibernateTemplate()时,会获得一个null的空指针异常。
  2. 在DAO的sql语句中,from后面的应该是POJO bean的类名,而不是数据库的表名。

也就是说,在引入spring的ioc时,所有的bean都在里面统一管理配置。在action里获得的那个dao并不是从spring的bean管理中(applicationContext.xml)获得的,因此也就没有获得象datasource等资源,改正办法,在action里注入dao的引用就可以了。
Spring不是执行时通过web.xml进行配置的,是在启动服务时就配好了的,所以在执行时只需调用注入进来的,如果这时候再new一个对象,是不会出错,只不过这个对象不是Spring注入进来的对象,所以在获得SessionFactory时抛出空指针。

展开阅读全文

spring注入不了对象、报空指针异常

03-20

[color=#FF0000]spring配置文件[/color]rnrn rn rn classpath:hibernate.cfg.xmlrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrn[color=#FF0000]action代码[/color]rnpackage com.ywt.web.action;rnrnimport java.util.List;rnrnimport com.opensymphony.xwork2.ActionSupport;rnimport com.ywt.bean.Bug_detail;rnimport com.ywt.bean.Bug_project;rnimport com.ywt.service.Bug_detailService;rnimport com.ywt.service.Bug_projectService;rnpublic class Bug_detailAction extends ActionSupport rnrn rn private static final long serialVersionUID = 1L;rn private List list;rn private Bug_detailService bug_detailService; rn private Bug_projectService bug_projectService;rn private List list2;rn public String getBUGAll()rn try rn list = bug_detailService.getAll();rn list2 = bug_projectService.getProject();rn return SUCCESS;rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn return INPUT;rn rn rn rn public List getList() rn return list;rn rn public void setList(List list) rn this.list = list;rn rnrn public List getList2() rn return list2;rn rnrnrnrnrn public void setList2(List list2) rn this.list2 = list2;rn rnrnrnrnrn public Bug_detailService getBug_detailService() rn return bug_detailService;rn rnrnrnrnrn public void setBug_detailService(Bug_detailService bug_detailService) rn this.bug_detailService = bug_detailService;rn rnrn public Bug_projectService getBug_projectService() rn return bug_projectService;rn rnrn public void setBug_projectService(Bug_projectService bug_projectService) rn this.bug_projectService = bug_projectService;rn rn rnrnrn[color=#FF0000]service[/color]rnrnpackage com.ywt.service.impl;rnrnimport java.util.List;rnimport com.ywt.bean.Bug_detail;rnimport com.ywt.dao.Bug_detailDao;rnimport com.ywt.service.Bug_detailService;rnrnpublic class Bug_detailServiceImpl implements Bug_detailService rn rn private Bug_detailDao detailDao;rn rn rn public List getAll() rn rn return detailDao.getAll();rn rnrn public List getProjectBug(Integer pid) rn rn return detailDao.getProjectBug(pid);rn rnrn public boolean addBug(Bug_detail detail) rn rn return detailDao.addBug(detail);rn rnrn rn rn rn public Bug_detailDao getDetailDao() rn return detailDao;rn rnrn public void setDetailDao(Bug_detailDao detailDao) rn this.detailDao = detailDao;rn rn rnrnrnrnrnpackage com.ywt.service.impl;rnrnimport java.util.List;rnrnimport com.ywt.bean.Bug_project;rnimport com.ywt.dao.Bug_projectDao;rnimport com.ywt.dao.impl.Bug_projectDaoImpl;rnimport com.ywt.service.Bug_projectService;rnrnpublic class Bug_projectServiceImpl implements Bug_projectService rn rn private Bug_projectDao projectDao ;rn rn rn rn public List getProject() rn rn return projectDao.getProject();rn rnrnrnrn public Bug_projectDao getProjectDao() rn return projectDao;rn rnrnrnrn public void setProjectDao(Bug_projectDao projectDao) rn this.projectDao = projectDao;rn rnrnrnrnrnrn[color=#FF0000]dao层[/color]rnrnpackage com.ywt.dao.impl;rnrnimport java.util.List;rnrnimport org.hibernate.HibernateException;rnimport org.hibernate.Query;rnimport org.hibernate.Session;rnimport org.springframework.orm.hibernate3.support.HibernateDaoSupport;rnimport com.ywt.bean.Bug_detail;rnimport com.ywt.dao.Bug_detailDao;rnrnpublic class Bug_detailDaoImpl extends HibernateDaoSupport implements Bug_detailDao rnrn @SuppressWarnings("unchecked")rn public List getAll() rn Session session = null;rn try rn String hql = "from Bug_detail";rn session = this.getSessionFactory().openSession();rn Query query = session.createQuery(hql);rn return query.list();rn catch (HibernateException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finallyrn session.close();rn rn return null;rn rn rnrn rn rn @SuppressWarnings("unchecked")rn public List getProjectBug(Integer pid) rn Session session = null;rn try rn String hql = "from Bug_detail as b where b.p_id = ?";rn session = this.getSessionFactory().openSession();rn Query query = session.createQuery(hql);rn query.setInteger(0, pid);rn return query.list();rn catch (HibernateException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finallyrn session.close();rn rn return null;rn rn rn rn rn rn rnrn public boolean addBug(Bug_detail detail) rn boolean flg = true;rn Session session = null;rn try rn rn session = this.getSessionFactory().openSession();rn session.beginTransaction().begin();rn session.save(detail);rn catch (HibernateException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn session.beginTransaction().rollback();rn flg = false;rn finallyrn session.beginTransaction().commit();rn session.close();rn rn rn return flg;rn rnrnrnrnrnpackage com.ywt.dao.impl;rnrnimport java.util.List;rnrnimport org.hibernate.HibernateException;rnimport org.hibernate.Query;rnimport org.hibernate.Session;rnimport org.springframework.orm.hibernate3.support.HibernateDaoSupport;rnrnimport com.ywt.bean.Bug_project;rnimport com.ywt.dao.Bug_projectDao;rnrnpublic class Bug_projectDaoImpl extends HibernateDaoSupport implements Bug_projectDao rn rn public List getProject() rn Session session = null;rn try rn String hql = "from Bug_project";rn session = this.getSessionFactory().openSession();rn Query query = session.createQuery(hql);rn return query.list();rn catch (HibernateException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finallyrn session.close();rn rn return null;rn rnrnrn 论坛

没有更多推荐了,返回首页