使用控制器创建bean时出错面临自动连线和限定符注解的问题

c86crjj0  于 2021-07-16  发布在  Java
关注(0)|答案(2)|浏览(375)

我正在创建一个预约医生的应用程序。在这里我有七节课在波乔班
在com.capg.bookmydoctor.dto包中,我有7个pojo类:

com.capg.bookmydoctor.dto

Admin.java
Appointment.java
Availability.java
Doctor.java
Feedback.java
Patient.java
User.java

在dao内部,我得到了5个dao方法接口和5个dao存储库接口

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接口和一个dao存储库接口代码,以减少问题。其余的接口是相似的。

IAppointmentDao.java
IAppointmentRepository.java

IDoctorDao.java
IDoctorRepository.java

IFeedbackDao.java
IFeedbackRepository.java

IPatientDao.java
IPatientRepository.java

在服务包中,我得到了5个服务方法接口和5个服务实现类
我猜由于这些服务类,我得到了错误,因为我的主类无法找到服务类,即使我使用了@autowired和@qualifier注解。

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);
    }

    }

在这里,我只嵌入一个服务实现类代码,使问题变得很小。其余的服务实现类是相似的,只是变量和方法不同。

IAppointmentService.java
IAppointmentServiceImpl.java

IDoctorService.java
IDoctorServiceImpl.java

IFeedbackService.java
IFeedbackServiceImpl.java

IPatientService.java
IPatientServiceImpl.java

在控制器包中,我得到了一个类

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 展位申请课程

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”。
在这里,我为所有服务实现类使用了@service annotation,并在其中指定了服务接口名称。
我使用@qualifier注解和@autowired,因为我得到了不同的respository和服务实现类。
尽管做了这些,我还是在控制台里发现了这些错误
错误:

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$0(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$0(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

请帮我摆脱这些错误
谢谢您。。。

6ss1mwsb

6ss1mwsb1#

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>{

}

在服务类 IAdminServiceImpl ,你的自动连线错误 Qualifier :

@Autowired
    @Qualifier("IAdminRepository")
    IAdminRepository adminDao;

应该是:

@Autowired
    @Qualifier("IAdminDao")
    IAdminRepository adminDao;

您应该在其他dao/服务类中检查这一点。

roqulrg3

roqulrg32#

你一直用错了注解。如果您将名称传递给@repository(“iadmindao”)这样的注解,那么您将创建名为“iadmindao”的bean,该bean应该通过使用@autowire@qualifier(iadmindao)自动连接,但是您所做的是@qualifier(“iadmindao”),因为没有具有此名称的bean,所以您将得到错误。
如果接口只有一个实现,请删除所有限定符,并从接口/类上使用的注解中删除名称,例如write@repository,@service only。然后使用@autowired。如果bean依赖是唯一的问题,那么应该启动应用程序。或者使用spring文档中描述的bean命名。

相关问题