Spring Boot 应用程序中没有类型 'testgroup.private_clinic.dao.PatientDAO' 的合格 bean
No qualifying bean of type 'testgroup.private_clinic.dao.PatientDAO' available in SpringBoot Application
我正在开发 crud 应用程序,在使用 springboot 时遇到困难,启动失败。这是我得到的:
20764 WARNING [main] --- org.springframework.context.annotation.AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'patientServiceImpl': Unsatisfied dependency expressed through method 'setPatientDAO' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'testgroup.private_clinic.dao.PatientDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
项目结构截图:
型号:
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);
}
}
主要class:
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使用class路径组件扫描的意思,你的入口点class就是SpringBootClass 将扫描其 class 路径中的所有 bean,除非您将其配置为不这样做。
查看您的项目结构,SpringBootClass 在 testgroup.private_clinic.service 包下,因此 Spring Boot 只会扫描这个包中的 bean,它只会找到 PatientServiceImpl 然而,在将它注入应用程序上下文之前,它需要先注入它的依赖项 PatientDAO 不是 testgroup.private_clinic.service 包的一部分,因此解释了您的错误。
您有两种选择来解决此问题:
将您的 SpringBootClass 移动到基本包 testgroup.private_clinic - 这将对这个包及其子包下的所有组件进行 Spring 引导扫描(例如, 服务,dao)
在你的 SpringBootClass 上使用 @ComponentScan 并在那里定义基础包你想扫描豆子。
@ComponentScan(basePackages = "testgroup.private_clinic")
感谢和欢呼!
我正在开发 crud 应用程序,在使用 springboot 时遇到困难,启动失败。这是我得到的:
20764 WARNING [main] --- org.springframework.context.annotation.AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'patientServiceImpl': Unsatisfied dependency expressed through method 'setPatientDAO' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'testgroup.private_clinic.dao.PatientDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
项目结构截图:
型号:
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);
}
}
主要class:
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使用class路径组件扫描的意思,你的入口点class就是SpringBootClass 将扫描其 class 路径中的所有 bean,除非您将其配置为不这样做。
查看您的项目结构,SpringBootClass 在 testgroup.private_clinic.service 包下,因此 Spring Boot 只会扫描这个包中的 bean,它只会找到 PatientServiceImpl 然而,在将它注入应用程序上下文之前,它需要先注入它的依赖项 PatientDAO 不是 testgroup.private_clinic.service 包的一部分,因此解释了您的错误。
您有两种选择来解决此问题:
将您的 SpringBootClass 移动到基本包 testgroup.private_clinic - 这将对这个包及其子包下的所有组件进行 Spring 引导扫描(例如, 服务,dao)
在你的 SpringBootClass 上使用 @ComponentScan 并在那里定义基础包你想扫描豆子。
@ComponentScan(basePackages = "testgroup.private_clinic")
感谢和欢呼!