Java web应用程序中使用Hibernate和Spring的问题
我在尝试获取CurrentSession()时遇到NullPointerException 我使用Tomcat5.5 index.jsp页面:Java web应用程序中使用Hibernate和Spring的问题,java,hibernate,spring,Java,Hibernate,Spring,我在尝试获取CurrentSession()时遇到NullPointerException 我使用Tomcat5.5 index.jsp页面: <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%@ page import="java.util.List" %>
<%@ page import="data.Singer" %>
<jsp:useBean id="singer" class="data.Singer" scope="session"/>
<jsp:setProperty name="singer" property="*" />
<form action="ControlServlet" method="POST">
<form method=“POST”>
Name:<br />
<input type=“text” name="name" /><br />
Type:<br />
<input type=“text” name="type" /><br />
<input type="submit" name="Add song" value="Add song">
<input type="submit" name="save" value="Save" /><br><br>
<input type ="submit" name="values" value="Get values" >
</form>
</body>
</html>
和SingerDao.java
public class SingerDao implements SingerDaoInterface {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List getDBValues() {
Session session = getCurrentSession();
List<Singer> singers = session.createCriteria(Singer.class).list();
return singers;
}
private org.hibernate.classic.Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public void updateSinger(Singer singer) {
Session session = getCurrentSession();
session.update(singer);
}
public Singer getSinger(int id) {
Singer singer = null;
Session session = getCurrentSession();
singer = (Singer) session.load(Singer.class, id);
return singer;
}
public void deleteSinger(Singer singer) {
Session session = getCurrentSession();
session.delete(singer);
}
public void insertRow(Singer singer) {
Session session = getCurrentSession();
session.save(singer);
}
}
公共类SingerDao实现SingerDao接口{
@自动连线
私人会话工厂会话工厂;
public void setSessionFactory(SessionFactory SessionFactory){
this.sessionFactory=sessionFactory;
}
公共列表getDBValues(){
会话会话=getCurrentSession();
List singers=session.createCriteria(Singer.class.List();
回归歌手;
}
private org.hibernate.classic.Session getCurrentSession(){
返回sessionFactory.getCurrentSession();
}
公共无效更新歌手(歌手){
会话会话=getCurrentSession();
更新(辛格);
}
公共歌手getSinger(国际id){
Singer=null;
会话会话=getCurrentSession();
singer=(singer)session.load(singer.class,id);
回归歌手;
}
公众歌手(歌手){
会话会话=getCurrentSession();
删除(歌手);
}
公共空间插入行(歌手){
会话会话=getCurrentSession();
拯救(歌手);
}
}
在简单的Java项目中,它工作得很好。我认为sessionFactory不会自动连线,但为什么呢?
谢谢大家。您不能在普通servlet中使用
@Autowired
,因为它们不是由Spring管理的
但是,您可以使用以下技巧手动启动servlet的自动连接:
public class ControlServlet extends HttpServlet {
@Autowired
private SingerDao singerdao;
public void init(ServletConfig cfg) {
super.init(cfg);
WebApplicationContextUtils
.getWebApplicationContext(cfg.getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
}
...
}
此外,正如评论中所建议的,由于您的设置中已经有Spring MVC的
DispatcherServlet
,因此最好将ControlServlet
的功能实现为Spring MVC控制器,而不是servlet(除非您有充分的理由将其作为servlet)。这当然是您尝试调用singerdao对象的方法的地方
这就是没有被注入的属性。这并不奇怪,因为Servlet是由web容器实例化的,而不是由Spring实例化的。Spring对这个servlet一无所知,也无法向其中注入任何内容。SingerDao类上有注释吗?它应该用@Repository注释。您可能应该添加一个附录,告诉他不要创建servlet,而应该创建SpringMVC控制器或其他mvc技术。只是为了朝正确的方向行驶。太感谢你了!你的建议对我帮助很大!
public class ControlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private SingerDao singerdao;
public SingerDao getSingerDao() {
return singerdao;
}
public void setSingerDao(SingerDao singerdao) {
this.singerdao = singerdao;
}
public ControlServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("values") != null) {
response.getWriter().println(singerdao.getDBValues());
}
}
}
public class SingerDao implements SingerDaoInterface {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List getDBValues() {
Session session = getCurrentSession();
List<Singer> singers = session.createCriteria(Singer.class).list();
return singers;
}
private org.hibernate.classic.Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public void updateSinger(Singer singer) {
Session session = getCurrentSession();
session.update(singer);
}
public Singer getSinger(int id) {
Singer singer = null;
Session session = getCurrentSession();
singer = (Singer) session.load(Singer.class, id);
return singer;
}
public void deleteSinger(Singer singer) {
Session session = getCurrentSession();
session.delete(singer);
}
public void insertRow(Singer singer) {
Session session = getCurrentSession();
session.save(singer);
}
}
public class ControlServlet extends HttpServlet {
@Autowired
private SingerDao singerdao;
public void init(ServletConfig cfg) {
super.init(cfg);
WebApplicationContextUtils
.getWebApplicationContext(cfg.getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
}
...
}