问题描述
我正在开发 crud 应用程序,在使用 springboot 时遇到了困难,启动失败。这是我得到的:
20764 警告 [main] --- org.springframework.context.annotation.AnnotationConfigApplicationContext:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“patientServiceImpl”的 bean 时出错:通过方法'setPatientDAO'参数0表示的不满意的依赖;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDeFinitionException:没有可用的“testgroup.private_clinic.dao.PatientDAO”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。依赖注释:{}
型号:
package testgroup.private_clinic.model;
import javax.persistence.*;
@Entity
@Table(name="Patients")
public class Patient {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name="patient_name")
String name;
@Column(name = "patient_surname")
String surname;
@Column(name = "patient_patronimic")
String patronimic;
@Column(name="adress")
String adress;
@Column(name = "status")
String status;
@Column(name="diagnosis")
String diagnosis;
//+getters and setters
控制器:
package testgroup.private_clinic.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import testgroup.private_clinic.model.Patient;
import testgroup.private_clinic.service.PatientService;
import java.util.List;
@RestController
public class PatientController {
PatientService patientService;
@Autowired
public void setPatientService(PatientService patientService){
this.patientService = patientService;
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView allPatients(){
List<Patient> patients = patientService.allPatients();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("patients");
modelAndView.addobject("patientList",patients);
return modelAndView;
}
@RequestMapping(value= "/edit{id}",method = RequestMethod.GET)
public ModelAndView editPage(@PathVariable("id") int id){
Patient patient = patientService.getByID(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("editPage");
modelAndView.addobject("patient",patient);
return modelAndView;
}
@RequestMapping(value="/edit",method = RequestMethod.POST)
public ModelAndView editPatient(@modelattribute("patient") Patient patient){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:/");
patientService.edit(patient);
return modelAndView;
}
}
存储库:
package testgroup.private_clinic.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import testgroup.private_clinic.model.Patient;
import javax.transaction.Transactional;
import java.util.*;
@Repository
public class PatientDAOImpl implements PatientDAO {
SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public List<Patient> allPatients() {
Session session = sessionFactory.getCurrentSession();
return session.createquery("from Patient").list();
}
@Override
@Transactional
public void add(Patient patient) {
Session session = sessionFactory.getCurrentSession();
session.persist(patient);
}
@Override
@Transactional
public void delete(Patient patient) {
Session session = sessionFactory.getCurrentSession();
session.delete(patient);
}
@Override
@Transactional
public void edit(Patient patient) {
Session session = sessionFactory.getCurrentSession();
session.update(patient);
}
@Override
@Transactional
public Patient getByID(int id) {
Session session = sessionFactory.getCurrentSession();
return session.get(Patient.class,id);
}
}
服务:
package testgroup.private_clinic.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import testgroup.private_clinic.model.Patient;
import testgroup.private_clinic.dao.PatientDAO;
import javax.transaction.Transactional;
import java.util.List;
@Service
public class PatientServiceImpl implements PatientService{
PatientDAO patientDAO;
@Autowired
public void setPatientDAO(PatientDAO patientDAO){
this.patientDAO = patientDAO;
}
@Transactional
@Override
public List<Patient> allPatients() {
return patientDAO.allPatients();
}
@Transactional
@Override
public void add(Patient patient) {
patientDAO.add(patient);
}
@Transactional
@Override
public void delete(Patient patient) {
patientDAO.delete(patient);
}
@Transactional
@Override
public void edit(Patient patient) {
patientDAO.edit(patient);
}
@Transactional
@Override
public Patient getByID(int id) {
return patientDAO.getByID(id);
}
}
主类:
package testgroup.private_clinic.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootClass {
public static void main(String[] args){
SpringApplication.run(testgroup.private_clinic.service.SpringBootClass.class,args);
}
}
解决方法
Spring Boot 使用类路径组件扫描意味着,您的入口点类 SpringBootClass 将扫描其类路径中的所有 bean,除非您将其配置为不这样做。
查看您的项目结构,SpringBootClass 位于 testgroup.private_clinic.service 包下,因此 Spring Boot 只会扫描此包中的 bean,并且只找到 >PatientServiceImpl 但是,在将其注入应用程序上下文之前,它需要首先注入其依赖项 PatientDAO,它不属于 testgroup.private_clinic.service > 因此打包,解释了您的错误。
您有两种方法可以解决此问题:
-
将您的 SpringBootClass 移动到基础包 testgroup.private_clinic - 这将使 Spring Boot 扫描此包及其子包下的所有组件( 例如服务、dao)
-
在您的 SpringBootClass 上使用 @ComponentScan 并在那里定义要扫描 bean 的基本包。
@ComponentScan(basePackages = "testgroup.private_clinic")
谢谢和欢呼!