存储库/实体的 Spring Boot 错误,“不是托管类型”

问题描述 投票:0回答:1

我知道之前在这个论坛上也有人问过类似的问题,但是提出的解决方案都没有帮助我,所以我写了一个单独的问题。代码来自 youtube 上的 spring boot 课程 (https://youtu.be/zvR-Oif_nxg)(我卡在了 2:21:00 左右),但是当我尝试在春季启动网站。错误全文为:


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

    }
}

部门.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> {}

部门服务.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
,则没有错误,但是新表不会在 h2 数据库中创建。另外,在官网的指南中,一直使用
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>


spring spring-boot inversion-of-control
1个回答
0
投票

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

解决方案

向构造函数添加公共标识符或将其删除。一个没有参数的公共构造函数将由 Java 在内部创建(如果没有其他构造函数存在)。

© www.soinside.com 2019 - 2024. All rights reserved.