上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException
我正在开发 spring 启动应用程序。在尝试了许多解决方案后,它没有得到 resolved.Please 帮助:
控制台:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'doctorController' 的 bean 时出错:通过字段 'doctorService' 表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'doctorService' 的 bean 时出错:通过字段 'doctorRepo' 表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'doctorRepository' 的 bean 时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:未找到类型 Doctor 的 属性 id!
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorService': Unsatisfied dependency expressed through field 'doctorRepo';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'doctorRepository' 的 bean 时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:未找到类型 Doctor 的 属性 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);
}
}
服务class:
@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);
}
型号class:
@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
pom.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>
Doctor的Id是doctorId,所以
Doctor findById(...
void deleteById(...
应该是
Doctor findByDoctorId(...
void deleteByDoctorId(...
或将 doctorId
更改为简单的 id
,使用 getter 和 setter。
我正在开发 spring 启动应用程序。在尝试了许多解决方案后,它没有得到 resolved.Please 帮助:
控制台: org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'doctorController' 的 bean 时出错:通过字段 'doctorService' 表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'doctorService' 的 bean 时出错:通过字段 'doctorRepo' 表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'doctorRepository' 的 bean 时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:未找到类型 Doctor 的 属性 id!
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorService': Unsatisfied dependency expressed through field 'doctorRepo';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'doctorRepository' 的 bean 时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:未找到类型 Doctor 的 属性 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);
}
}
服务class:
@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);
}
型号class:
@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
pom.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>
Doctor的Id是doctorId,所以
Doctor findById(...
void deleteById(...
应该是
Doctor findByDoctorId(...
void deleteByDoctorId(...
或将 doctorId
更改为简单的 id
,使用 getter 和 setter。