使用控制器创建 bean 时出错。面对自动装配和限定符注释的问题
Error creating bean with controller. Facing issues with autowired and qualifier annotations
我正在创建一个预约医生预约的应用程序。这里我在 pojo class
中得到了 7 classes
包装内 com.capg.bookmydoctor.dto 我有 7 个 pojo classes :
com.capg.bookmydoctor.dto
Admin.java
Appointment.java
Availability.java
Doctor.java
Feedback.java
Patient.java
User.java
在 Dao 内部,我得到了 5 个 Dao 方法接口和 5 个 Dao Repository 接口
com.capg.bookmydoctor.dao
IAdminDao.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.dao;
import com.capg.bookmydoctor.dto.Admin;
public interface IAdminDao {
public Admin addAdmin(Admin admin);
public Admin updateAdmin(Admin admin);
public Admin removeAdmin(Admin admin);
public Admin viewAdmin(Admin admin);
}
-------------------------------------------------------------------------------------
IAdminDaoRepository.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.capg.bookmydoctor.dto.Admin;
@Repository("IAdminDao")
public interface IAdminRepository extends IAdminDao, JpaRepository<Admin, Integer>{
}
这里我只嵌入了一个Dao Interface和一个Dao Repository接口代码,使问题变小。其余界面类似。
IAppointmentDao.java
IAppointmentRepository.java
IDoctorDao.java
IDoctorRepository.java
IFeedbackDao.java
IFeedbackRepository.java
IPatientDao.java
IPatientRepository.java
在服务包中,我得到了 5 个服务方法接口和 5 个服务实现 classes
我猜是由于这些服务 classes 我收到错误,因为我的主要 class 无法找到服务 class 即使我使用了 @Autowired 和 @限定符注释。
com.capg.bookmydoctor.service
IAdminService.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.service;
import com.capg.bookmydoctor.dto.Admin;
public interface IAdminService {
public Admin addAdmin(Admin admin);
public Admin updateAdmin(Admin admin);
public Admin removeAdmin(Admin admin);
public Admin viewAdmin(Admin admin);
}
-------------------------------------------------------------------------------------
IAdminServiceImpl.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.capg.bookmydoctor.dao.IAdminRepository;
import com.capg.bookmydoctor.dto.Admin;
@Service("IAdminService")
public class IAdminServiceImpl implements IAdminService {
@Autowired
@Qualifier("IAdminRepository")
IAdminRepository adminDao;
@Override
public Admin addAdmin(Admin admin) {
return adminDao.addAdmin(admin);
}
@Override
public Admin updateAdmin(Admin admin) {
return adminDao.updateAdmin(admin);
}
@Override
public Admin removeAdmin(Admin admin) {
return adminDao.removeAdmin(admin);
}
@Override
public Admin viewAdmin(Admin admin) {
return adminDao.viewAdmin(admin);
}
}
这里我只嵌入了一个服务实现 class 代码以使问题变小。其余服务实现 class 相似,只是变量和方法不同。
IAppointmentService.java
IAppointmentServiceImpl.java
IDoctorService.java
IDoctorServiceImpl.java
IFeedbackService.java
IFeedbackServiceImpl.java
IPatientService.java
IPatientServiceImpl.java
在控制器包里面我有一个class
com.capg.bookmydoctor.controller
BookMyDoctorController.java
package com.capg.bookmydoctor.controller;
import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.capg.bookmydoctor.dto.Admin;
import com.capg.bookmydoctor.dto.Appointment;
import com.capg.bookmydoctor.dto.AvailabilityDates;
import com.capg.bookmydoctor.dto.Doctor;
import com.capg.bookmydoctor.dto.FeedBack;
import com.capg.bookmydoctor.dto.Patient;
import com.capg.bookmydoctor.service.IAdminService;
import com.capg.bookmydoctor.service.IAppointmentService;
import com.capg.bookmydoctor.service.IDoctorService;
import com.capg.bookmydoctor.service.IFeedbackService;
import com.capg.bookmydoctor.service.IPatientService;
@RestController
@RequestMapping("/doctor")
public class BookMyDoctorController {
@Autowired
@Qualifier("IAdminService")
IAdminService adminService;
@Autowired
@Qualifier("IAppointmentService")
IAppointmentService appointmentService;
@Autowired
@Qualifier("IDoctorService")
IDoctorService doctorService;
@Autowired
@Qualifier("IFeedbackService")
IFeedbackService feedbackService;
@Autowired
@Qualifier("IPatientService")
IPatientService patientService;
@PostMapping("/admin")
public Admin addAdmin(@RequestBody Admin admin) {
return adminService.addAdmin(admin);
}
@PutMapping("/admin")
public Admin updateAdmin(@RequestBody Admin admin) {
return adminService.updateAdmin(admin);
}
@DeleteMapping("/admin")
public Admin removeAdmin(@RequestBody Admin admin) {
return adminService.removeAdmin(admin);
}
@GetMapping("/admin")
public Admin viewAdmin(@RequestBody Admin admin) {
return adminService.viewAdmin(admin);
}
@GetMapping("/appointment")
public List<Appointment> getAllAppointments() {
return appointmentService.getAllAppointments();
}
@GetMapping("/feedback/all")
public List<FeedBack> getAllFeedback(@RequestBody Doctor doc) {
return feedbackService.getAllFeedback(doc);
}
@PostMapping("/patient")
public Patient addPatient(@RequestBody Patient bean) {
return patientService.addPatient(bean);
}
@PutMapping("/patient")
public Patient editPatientProfile(@RequestBody Patient bean) {
return patientService.editPatientProfile(bean);
}
@DeleteMapping("/patient")
public Patient removePatientDetails(@RequestBody Patient bean) {
return patientService.removePatientDetails(bean);
}
@GetMapping("/patient")
public Patient getPatient(@RequestBody Patient patient) {
return patientService.getPatient(patient);
}
@GetMapping("/patient/all")
public List<Patient> getAllPatient() {
return patientService.getAllPatient();
}
@GetMapping("/patient/doctor")
public List<Patient> getPatientListByDoctor(@RequestBody Doctor doctor) {
return patientService.getPatientListByDoctor(doctor);
}
@GetMapping("/patient/{appdate}")
public List<Patient> getPatientListByDate(@PathVariable LocalDate appdate) {
return patientService.getPatientListByDate(appdate);
}
包裹里面com.capg.bookmydoctor我拿到了spring展位申请class
com.capg.bookmydoctor
package com.capg.bookmydoctor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.capg.bookmydoctor.controller,com.capg.bookmydoctor.dao,com.capg.bookmydoctor.service,com.capg.bookmydoctor.dto")
public class BookMyDoctorAppApplication {
public static void main(String[] args) {
SpringApplication.run(BookMyDoctorAppApplication.class, args);
}
}
这里我对所有包都使用了相同的包名“com.capg.bookmydoctor”。
这里我对所有服务实现class都使用了@Service注解,并在其中指定了服务接口名称。
我使用了@Qualifier 注释和@Autowired,因为我得到了不同的存储库和服务实现classes。
尽管做了所有这些,但我在控制台中收到了这些错误
错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookMyDoctorController': Unsatisfied dependency expressed through field 'adminService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'IAdminService': Unsatisfied dependency expressed through field 'adminDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capg.bookmydoctor.dao.IAdminRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=IAdminRepository)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=17=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.5.jar:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.5.jar:5.3.5]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) [spring-boot-2.4.4.jar:2.4.4]
at com.capg.bookmydoctor.BookMyDoctorAppApplication.main(BookMyDoctorAppApplication.java:12) [classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'IAdminService': Unsatisfied dependency expressed through field 'adminDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capg.bookmydoctor.dao.IAdminRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=IAdminRepository)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=17=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.5.jar:5.3.5]
... 21 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capg.bookmydoctor.dao.IAdminRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=IAdminRepository)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.5.jar:5.3.5]
... 35 common frames omitted
请帮助我摆脱这些错误
谢谢...
在IAdminRepository
中,bean的名称是"IAdminDao"
。
package com.capg.bookmydoctor.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.capg.bookmydoctor.dto.Admin;
@Repository("IAdminDao")
public interface IAdminRepository extends IAdminDao, JpaRepository<Admin, Integer>{
}
在服务 class IAdminServiceImpl
中,您的自动装配错误 Qualifier
:
@Autowired
@Qualifier("IAdminRepository")
IAdminRepository adminDao;
应该是:
@Autowired
@Qualifier("IAdminDao")
IAdminRepository adminDao;
你应该在其他 DAO/Service classes.
中检查这个
你一直都用错了注解。如果您将名称传递给 @Repository("IAdminDao") 之类的注释,那么您正在创建名称为“IAdminDao”的 bean,它应该使用 @Autowire @Qualifier(IAdminDao) 自动装配,但您正在做的是 @Qualifier("IAdminRepository")因为没有这个名称的 bean,所以你会收到错误。
如果接口只有一个实现,则删除所有限定符,并从 interface/classes 上使用的注释中删除名称,例如。只写@Repository,@Service。然后使用@Autowired。如果 bean 依赖性是唯一的问题,这应该会启动您的应用程序。或者使用 spring 文档中描述的 bean 命名。
我正在创建一个预约医生预约的应用程序。这里我在 pojo class
中得到了 7 classes包装内 com.capg.bookmydoctor.dto 我有 7 个 pojo classes :
com.capg.bookmydoctor.dto
Admin.java
Appointment.java
Availability.java
Doctor.java
Feedback.java
Patient.java
User.java
在 Dao 内部,我得到了 5 个 Dao 方法接口和 5 个 Dao Repository 接口
com.capg.bookmydoctor.dao
IAdminDao.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.dao;
import com.capg.bookmydoctor.dto.Admin;
public interface IAdminDao {
public Admin addAdmin(Admin admin);
public Admin updateAdmin(Admin admin);
public Admin removeAdmin(Admin admin);
public Admin viewAdmin(Admin admin);
}
-------------------------------------------------------------------------------------
IAdminDaoRepository.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.capg.bookmydoctor.dto.Admin;
@Repository("IAdminDao")
public interface IAdminRepository extends IAdminDao, JpaRepository<Admin, Integer>{
}
这里我只嵌入了一个Dao Interface和一个Dao Repository接口代码,使问题变小。其余界面类似。
IAppointmentDao.java
IAppointmentRepository.java
IDoctorDao.java
IDoctorRepository.java
IFeedbackDao.java
IFeedbackRepository.java
IPatientDao.java
IPatientRepository.java
在服务包中,我得到了 5 个服务方法接口和 5 个服务实现 classes
我猜是由于这些服务 classes 我收到错误,因为我的主要 class 无法找到服务 class 即使我使用了 @Autowired 和 @限定符注释。
com.capg.bookmydoctor.service
IAdminService.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.service;
import com.capg.bookmydoctor.dto.Admin;
public interface IAdminService {
public Admin addAdmin(Admin admin);
public Admin updateAdmin(Admin admin);
public Admin removeAdmin(Admin admin);
public Admin viewAdmin(Admin admin);
}
-------------------------------------------------------------------------------------
IAdminServiceImpl.java
-------------------------------------------------------------------------------------
package com.capg.bookmydoctor.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.capg.bookmydoctor.dao.IAdminRepository;
import com.capg.bookmydoctor.dto.Admin;
@Service("IAdminService")
public class IAdminServiceImpl implements IAdminService {
@Autowired
@Qualifier("IAdminRepository")
IAdminRepository adminDao;
@Override
public Admin addAdmin(Admin admin) {
return adminDao.addAdmin(admin);
}
@Override
public Admin updateAdmin(Admin admin) {
return adminDao.updateAdmin(admin);
}
@Override
public Admin removeAdmin(Admin admin) {
return adminDao.removeAdmin(admin);
}
@Override
public Admin viewAdmin(Admin admin) {
return adminDao.viewAdmin(admin);
}
}
这里我只嵌入了一个服务实现 class 代码以使问题变小。其余服务实现 class 相似,只是变量和方法不同。
IAppointmentService.java
IAppointmentServiceImpl.java
IDoctorService.java
IDoctorServiceImpl.java
IFeedbackService.java
IFeedbackServiceImpl.java
IPatientService.java
IPatientServiceImpl.java
在控制器包里面我有一个class
com.capg.bookmydoctor.controller
BookMyDoctorController.java
package com.capg.bookmydoctor.controller;
import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.capg.bookmydoctor.dto.Admin;
import com.capg.bookmydoctor.dto.Appointment;
import com.capg.bookmydoctor.dto.AvailabilityDates;
import com.capg.bookmydoctor.dto.Doctor;
import com.capg.bookmydoctor.dto.FeedBack;
import com.capg.bookmydoctor.dto.Patient;
import com.capg.bookmydoctor.service.IAdminService;
import com.capg.bookmydoctor.service.IAppointmentService;
import com.capg.bookmydoctor.service.IDoctorService;
import com.capg.bookmydoctor.service.IFeedbackService;
import com.capg.bookmydoctor.service.IPatientService;
@RestController
@RequestMapping("/doctor")
public class BookMyDoctorController {
@Autowired
@Qualifier("IAdminService")
IAdminService adminService;
@Autowired
@Qualifier("IAppointmentService")
IAppointmentService appointmentService;
@Autowired
@Qualifier("IDoctorService")
IDoctorService doctorService;
@Autowired
@Qualifier("IFeedbackService")
IFeedbackService feedbackService;
@Autowired
@Qualifier("IPatientService")
IPatientService patientService;
@PostMapping("/admin")
public Admin addAdmin(@RequestBody Admin admin) {
return adminService.addAdmin(admin);
}
@PutMapping("/admin")
public Admin updateAdmin(@RequestBody Admin admin) {
return adminService.updateAdmin(admin);
}
@DeleteMapping("/admin")
public Admin removeAdmin(@RequestBody Admin admin) {
return adminService.removeAdmin(admin);
}
@GetMapping("/admin")
public Admin viewAdmin(@RequestBody Admin admin) {
return adminService.viewAdmin(admin);
}
@GetMapping("/appointment")
public List<Appointment> getAllAppointments() {
return appointmentService.getAllAppointments();
}
@GetMapping("/feedback/all")
public List<FeedBack> getAllFeedback(@RequestBody Doctor doc) {
return feedbackService.getAllFeedback(doc);
}
@PostMapping("/patient")
public Patient addPatient(@RequestBody Patient bean) {
return patientService.addPatient(bean);
}
@PutMapping("/patient")
public Patient editPatientProfile(@RequestBody Patient bean) {
return patientService.editPatientProfile(bean);
}
@DeleteMapping("/patient")
public Patient removePatientDetails(@RequestBody Patient bean) {
return patientService.removePatientDetails(bean);
}
@GetMapping("/patient")
public Patient getPatient(@RequestBody Patient patient) {
return patientService.getPatient(patient);
}
@GetMapping("/patient/all")
public List<Patient> getAllPatient() {
return patientService.getAllPatient();
}
@GetMapping("/patient/doctor")
public List<Patient> getPatientListByDoctor(@RequestBody Doctor doctor) {
return patientService.getPatientListByDoctor(doctor);
}
@GetMapping("/patient/{appdate}")
public List<Patient> getPatientListByDate(@PathVariable LocalDate appdate) {
return patientService.getPatientListByDate(appdate);
}
包裹里面com.capg.bookmydoctor我拿到了spring展位申请class
com.capg.bookmydoctor
package com.capg.bookmydoctor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.capg.bookmydoctor.controller,com.capg.bookmydoctor.dao,com.capg.bookmydoctor.service,com.capg.bookmydoctor.dto")
public class BookMyDoctorAppApplication {
public static void main(String[] args) {
SpringApplication.run(BookMyDoctorAppApplication.class, args);
}
}
这里我对所有包都使用了相同的包名“com.capg.bookmydoctor”。
这里我对所有服务实现class都使用了@Service注解,并在其中指定了服务接口名称。
我使用了@Qualifier 注释和@Autowired,因为我得到了不同的存储库和服务实现classes。
尽管做了所有这些,但我在控制台中收到了这些错误
错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookMyDoctorController': Unsatisfied dependency expressed through field 'adminService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'IAdminService': Unsatisfied dependency expressed through field 'adminDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capg.bookmydoctor.dao.IAdminRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=IAdminRepository)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=17=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.5.jar:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.5.jar:5.3.5]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313) [spring-boot-2.4.4.jar:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) [spring-boot-2.4.4.jar:2.4.4]
at com.capg.bookmydoctor.BookMyDoctorAppApplication.main(BookMyDoctorAppApplication.java:12) [classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'IAdminService': Unsatisfied dependency expressed through field 'adminDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capg.bookmydoctor.dao.IAdminRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=IAdminRepository)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=17=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.5.jar:5.3.5]
... 21 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capg.bookmydoctor.dao.IAdminRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=IAdminRepository)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.5.jar:5.3.5]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.5.jar:5.3.5]
... 35 common frames omitted
请帮助我摆脱这些错误
谢谢...
在IAdminRepository
中,bean的名称是"IAdminDao"
。
package com.capg.bookmydoctor.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.capg.bookmydoctor.dto.Admin;
@Repository("IAdminDao")
public interface IAdminRepository extends IAdminDao, JpaRepository<Admin, Integer>{
}
在服务 class IAdminServiceImpl
中,您的自动装配错误 Qualifier
:
@Autowired
@Qualifier("IAdminRepository")
IAdminRepository adminDao;
应该是:
@Autowired
@Qualifier("IAdminDao")
IAdminRepository adminDao;
你应该在其他 DAO/Service classes.
中检查这个你一直都用错了注解。如果您将名称传递给 @Repository("IAdminDao") 之类的注释,那么您正在创建名称为“IAdminDao”的 bean,它应该使用 @Autowire @Qualifier(IAdminDao) 自动装配,但您正在做的是 @Qualifier("IAdminRepository")因为没有这个名称的 bean,所以你会收到错误。
如果接口只有一个实现,则删除所有限定符,并从 interface/classes 上使用的注释中删除名称,例如。只写@Repository,@Service。然后使用@Autowired。如果 bean 依赖性是唯一的问题,这应该会启动您的应用程序。或者使用 spring 文档中描述的 bean 命名。