我是Spring的新手。当我试图查看localhost:8080/persons时,我收到错误
java.lang.NullPointerException: null
at by.bsuir.task.service.PersonServiceImpl.listPersons(PersonServiceImpl.java:32) ~[classes/:na]
at by.bsuir.task.controller.PersonController.listPersons(PersonController.java:31) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at ...
我认为问题出在PersonDAOImpl,方法listPersons(),session
得到NPE。但我不确定。
我的项目如下:
PersonServiceImpl
package by.bsuir.task.service;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import by.bsuir.task.dao.PersonDAO;
import by.bsuir.task.model.Person;
"personService"
public class PersonServiceImpl implements PersonService {
private PersonDAO personDAO;
public void setPersonDAO(PersonDAO personDAO) {
this.personDAO = personDAO;
}
@Transactional
public void addPerson(Person p) {
this.personDAO.addPerson(p);
}
@Transactional
public void updatePerson(Person p) {
this.personDAO.updatePerson(p);
}
@Transactional
public List<Person> listPersons() {
return this.personDAO.listPersons();
}
@Transactional
public Person getPersonById(int id) {
return this.personDAO.getPersonById(id);
}
@Transactional
public void removePerson(int id) {
this.personDAO.removePerson(id);
}
}
PersonDAOImpl
package by.bsuir.task.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import by.bsuir.task.model.Person;
@Repository
public class PersonDAOImpl implements PersonDAO {
private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
public void addPerson(Person p) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(p);
logger.info("Person saved successfully, Person Details="+p);
}
public void updatePerson(Person p) {
Session session = this.sessionFactory.getCurrentSession();
session.update(p);
logger.info("Person updated successfully, Person Details="+p);
}
"unchecked"
public List<Person> listPersons() {
Session session = this.sessionFactory.getCurrentSession();
List<Person> personsList = session.createQuery("from Person").list();
for(Person p : personsList){
logger.info("Person List::"+p);
}
return personsList;
}
public Person getPersonById(int id) {
Session session = this.sessionFactory.getCurrentSession();
Person p = (Person) session.load(Person.class, new Integer(id));
logger.info("Person loaded successfully, Person details="+p);
return p;
}
public void removePerson(int id) {
Session session = this.sessionFactory.getCurrentSession();
Person p = (Person) session.load(Person.class, new Integer(id));
if(null != p){
session.delete(p);
}
logger.info("Person deleted successfully, person details="+p);
}
}
PersonController
package by.bsuir.task.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import by.bsuir.task.model.Person;
import by.bsuir.task.service.PersonService;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
required=true
@Qualifier(value = "personService")
public void setPersonService(PersonService ps){
this.personService = ps;
}
value = "/persons", method = RequestMethod.GET
public String listPersons(Model model) {
List<Person> listOfUsers = this.personService.listPersons();
model.addAttribute("person", new Person());
model.addAttribute("listPersons", listOfUsers);
return "person";
}
//For add and update person both
value= "/person/add", method = RequestMethod.POST
public String addPerson(@ModelAttribute("person") Person p){
if(p.getId() == 0){
//new person, add it
this.personService.addPerson(p);
}else{
//existing person, call update
this.personService.updatePerson(p);
}
return "redirect:/persons";
}
"/remove/{id}"
public String removePerson(@PathVariable("id") int id){
this.personService.removePerson(id);
return "redirect:/persons";
}
"/edit/{id}"
public String editPerson(@PathVariable("id") int id, Model model){
model.addAttribute("person", this.personService.getPersonById(id));
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
}
似乎无法找到我的问题,我只是在学习Spring \ Hibernate
答案 0 :(得分:1)
您的问题中缺少一些代码。
对于班级PersonServiceImpl
,代码应如下所示:
@Service("personService")
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDAO personDAO;
// Some other code
}
将@Autowired
注释添加到PersonDAO
类而不这样做会导致NullPointerException
,因为您没有为该类创建任何对象,使用@Autowired
注释它将告诉{{1}}需要注射的春天。