存储库/实体的 Spring 启动错误,"not a managed type"



我知道以前在这个论坛上有人问过类似的问题,但是没有一个提出的解决方案对我有帮助,所以我写了一个单独的问题。该代码来自youtube上的spring引导课程(https://youtu.be/zvR-Oif_nxg)(im大约在2:21:00卡住),但是当我试图在spring引导网站上进行课程时也会出现此错误。错误的全文是:


"Error creating bean with name 'departmentController': Unsatisfied dependency expressed through field 'departmentService': Error creating bean with name 'departmentServiceImpl': Unsatisfied dependency expressed through field 'departmentRepository': Error creating bean with name 'departmentRepository' defined in com.kamilosinni.course.repository.DepartmentRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.kamilosinni.course.entity.Department"


主应用程序类(CourseApplication.java):


package com.kamilosinni.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApplication {
public static void main(String[] args) {
SpringApplication.run(CourseApplication.class, args);
}
}

DepartmentController.java:

package com.kamilosinni.course.controller;

import com.kamilosinni.course.entity.Department;
import com.kamilosinni.course.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@GetMapping("/departments")
public Department saveDepartment(@RequestBody Department department)
{
return departmentService.saveDepartment(department);
}
}

Department.java:

package com.kamilosinni.course.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;
private String departmentName;
private String departmentAdress;
private String departmentCode;
public Long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentAdress() {
return departmentAdress;
}
public void setDepartmentAdress(String departmentAdress) {
this.departmentAdress = departmentAdress;
}
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
public Department(Long departmentId, String departmentName, String departmentAdress, String departmentCode) {
this.departmentId = departmentId;
this.departmentName = departmentName;
this.departmentAdress = departmentAdress;
this.departmentCode = departmentCode;
}
public Department(){}
@Override
public String toString() {
return "Department{" +
"departmentId=" + departmentId +
", departmentName='" + departmentName + ''' +
", departmentAdress='" + departmentAdress + ''' +
", departmentCode='" + departmentCode + ''' +
'}';
}
}

DepartmentRepository.java:

package com.kamilosinni.course.repository;
import com.kamilosinni.course.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {}

DepartmentService.java:

package com.kamilosinni.course.service;
import com.kamilosinni.course.entity.Department;

public interface DepartmentService {
Department saveDepartment(Department department);
}

DepartmentServiceImpl.java:

package com.kamilosinni.course.service;
import com.kamilosinni.course.entity.Department;
import com.kamilosinni.course.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DepartmentServiceImpl implements DepartmentService{

@Autowired
DepartmentRepository departmentRepository;
DepartmentServiceImpl(){}
@Override
public Department saveDepartment(Department department) {
return departmentRepository.save(department);
}
}

我试图在主类中添加额外的注释:

package com.kamilosinni.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan("com.kamilosinni.course.entity")
@EnableJpaRepositories("com.kamilosinni.course.repository")
public class CourseApplication {
public static void main(String[] args) {
SpringApplication.run(CourseApplication.class, args);
}
}

我还试图将类移动到主类的主包中,并将@NoRepositoryBean注释添加到DepartmentRepository,这没有做任何事情

编辑:如果我在Department.java中将javax导入替换为jakarta,则不会出现错误。另外,在官方网站上的指南中,一直使用javax,这很奇怪,因为即使重写的代码也不能工作。可能是配置不正确?或者是依赖项。

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

我还添加了我的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>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kamilosinni</groupId>
<artifactId>course</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>course</name>
<description>Spring boot course</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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>com.h2database</groupId>
<artifactId>h2</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>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

此处的文件结构

在DepartmentServiceImpl类中没有公共构造函数。Spring需要它来进行依赖注入。

<<p>

解决方案/strong>为构造函数添加公共标识符或删除它。没有参数的公共构造函数将由Java内部创建(如果没有其他构造函数)。

将pom.xml添加到问题后:

请删除此依赖项:

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>

遇到同样的问题。

在我的例子中,问题是:

  1. 在错误的依赖项(javax.persistence:javax.persistence-api:2.2),删除它
  2. @Entity@Id的输入错误-替换为:
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

似乎去年javax.persistencejakarta.persistence取代了。无论如何,spring data JPA示例现在使用jakarta包

相关内容

最新更新