当前位置: 首页 > 知识库问答 >
问题:

上下文初始化期间遇到异常-取消刷新尝试:org。springframework。豆。工厂未满足的费用例外

寿鸣
2023-03-14

我正在开发一个Spring引导应用程序。在尝试了许多解决方案后,它没有得到解决。请帮助:

控制台:org。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“doctorController”的bean时出错:通过字段“doctorService”表示未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂UnsatisfiedDependencyException:创建名为“doctorService”的bean时出错:通过字段“doctorRepo”表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建名为“doctorRepository”的bean时出错:调用init方法失败;嵌套的异常是org。springframework。数据映射。PropertyReferenceException:找不到类型医生的属性id!

原因:org。springframework。豆。工厂UnsatisfiedDependencyException:创建名为“doctorService”的bean时出错:通过字段“doctorRepo”表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建名为“doctorRepository”的bean时出错:调用init方法失败;嵌套的异常是org。springframework。数据映射。PropertyReferenceException:找不到类型医生的属性id!

控制器:

@RestController
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @RequestMapping("/hms/doctor")
    public List<Doctor> getAllDoctor()
    {
        return doctorService.getAllDoctor();
    }

    @RequestMapping(method=RequestMethod.POST,value="hms/doctor")
    public void addDoctor(@RequestBody Doctor doctor)
    {
        doctorService.addDoctor(doctor);

    }
}

服务类别:

@Service
public class DoctorService {

    Logger logger= LoggerFactory.getLogger(DoctorService.class);

    @Autowired
    private DoctorRepository doctorRepo;

    public List<Doctor> getAllDoctor(){

        logger.error("error happened");
        logger.trace(" Error !!!");
        List<Doctor> doctor= new ArrayList<Doctor>();
        doctorRepo.findAll().forEach(doctor::add);      
        return doctor;

    }

    public Doctor getDoctorById(String doctorId) {
        return doctorRepo.findById(doctorId);       
    }

    public Doctor getDoctorByPhoneNumber(long phoneNumber)
    {
        return doctorRepo.findByPhoneNumber(phoneNumber);

    }


    public void addDoctor(Doctor doctor) {
        doctorRepo.save(doctor);                
    }

    public void updateDoctor(String doctorId,Doctor doctor) {
        doctorRepo.save(doctor);
    }
}

存储库:

public interface DoctorRepository extends CassandraRepository<Doctor, Integer>
{

    Doctor findById(String doctorId);

    Doctor findByPhoneNumber(long phoneNumber);

    void deleteById(String doctorId);

}

模型类:

@Entity
public class Doctor {

    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @PrimaryKey
    private int doctorId;
    private String doctorName;
    private long phoneNumber;
    private String specialization;
    private int totalExperience;
    private String workingDays[]= {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};

    public Doctor() {

    }

    public Doctor(int doctorId, String doctorName, long phoneNumber, String specialization, int totalExperience,
            String[] workingDays) {`enter code here`
        super();
        this.doctorId = doctorId;
        this.doctorName = doctorName;
        this.phoneNumber = phoneNumber;
        this.specialization = specialization;
        this.totalExperience = totalExperience;
        this.workingDays = workingDays;
// getters and setters 

波姆。xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.fullstack.assessment</groupId>
    <artifactId>hms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hms</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!--    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency> -->   
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

共有1个答案

益富
2023-03-14

医生的身份证是医生身份证,所以

 Doctor findById(...
 void deleteById(...

应该是

Doctor findByDoctorId(...
void deleteByDoctorId(...

或者使用getter和setter将doctorId更改为简单的id

 类似资料: