创建名为 'roleRepository' java spring 引导的 bean 时出错

Error creating bean with name 'roleRepository' java spring boot

RoleRepository

package com.jawad.api.repository;

import javax.management.relation.Role;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RoleRepository extends CrudRepository<Role,Integer>{

}

AppsController

package com.jawad.api.controller;

import java.util.List;

import javax.management.relation.Role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jawad.api.repository.RoleRepository;

@RestController
public class AppsController {
    
    @Autowired
    private RoleRepository rolerepository;
    
    @GetMapping("/getAllRoles")
    public List<Role> getAllRoles() {
        return (List<Role>) rolerepository.findAll();
    }
    
    @GetMapping("/hi")
    public String hi(){
        return "hi";
    }
}

PrepaidWebAppApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PrepaidWebAppApplication {

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

当我 运行 我的应用程序出现错误时,我找不到修复它的方法:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not a managed type: class javax.management.relation.Role
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1827) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1265) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:624) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:612) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:51) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:36) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at com.jawad.api.PrepaidWebAppApplication.main(PrepaidWebAppApplication.java:12) [classes/:na]
    Caused by: java.lang.IllegalArgumentException: Not a managed type: class javax.management.relation.Role
        at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
        at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
        at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:229) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:179) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:162) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:72) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:309) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:212) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:244) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:57) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:169) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        ... 24 common frames omitted

当我将 PrepaidWebAppApplication.java 移动到包含 3 个模型(用户、角色和机构)的模型文件夹时,它可以工作,但我的获取请求将不再被识别。我收到错误:404 not found.

我该如何解决这个问题?

您已经从 RoleRepositoryAppsController 中的 javax 包中导入了 Role 实体。从您的项目中导入它,它应该适合您。

import javax.management.relation.Role;

将上述导入更改为您的项目 Role 实体。

关于spring-boot-starter-data-jpa的一些基本概念:

  1. stereotype 注释如:
    A.@Component
    B. @Controller
    C. @Service
    D. @Repository
    在 spring 中必须是具体的 class.

  2. 最新版本的 Spring 引导已启用 @EnableJpaRepositories 自动启用 spring-boot-starter-data-jpa.

    的依赖项
  3. @EnableJpaRepository负责CrudRepositoryJPARepository等接口实现的注册。这里我们不需要添加SpringFramework.

    stereotype注解
  4. 对于接口 CrudRepositoryJPARepository 有它的 JPA 部分所以它引用的 class 必须有 JPA 注释,如 @Entity@Table 在混凝土上 class.

现在回到你的问题陈述

对于import javax.management.relation.Role

  1. import javax.management.relation.Role是具体的class,即不是抽象的class --> Accepted
  2. import javax.management.relation.Role class没有JPA注解,所以不能直接用CrudRepository, JPARepository ---> 未接受
  3. RoleRepository 不应有 stereotype 注释。

解法:

  1. 创建一个类似于 import javax.management.relation.Role 的 class,并在其上添加 JPA 注释。
  2. CrudRepositoryJPARepository 中使用的 JPA 实体必须包含 @Id 字段,否则您将遇到新的异常。

还请交叉检查您的实体 class,我错过了下面的 Id 注释,以及主键上生成的值注释,因此 org.springframework.beans.factory.BeanCreationException 发生在存储库上。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)