Error creating bean with name ‘roleRepository’ in Java Spring Boot.

huangapple go评论71阅读模式
英文:

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]
    	...
    	(更多错误信息)

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]
    	...
    	(更多错误信息)

当我将PrepaidWebAppApplication.java移动到包含3个模型(user、role和institution)的模型文件夹中时,它可以正常工作,但是我的GET请求不再被识别,我遇到了错误:404 not found

我该如何解决这个问题?

英文:

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&lt;Role,Integer&gt;{
}

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(&quot;/getAllRoles&quot;)
public List&lt;Role&gt; getAllRoles() {
return (List&lt;Role&gt;) rolerepository.findAll();
}
@GetMapping(&quot;/hi&quot;)
public String hi(){
return &quot;hi&quot;;
}
}

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

When I run my application I get an error and I cannot find a way to fix it:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;roleRepository&#39;: 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.&lt;init&gt;(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$5(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

When I moved the PrepaidWebAppApplication.java to the model folder containing 3 models (user, role, and institution) it works but then my get requests won't be recognized anymore. I get the error: 404 not found.

How can I solve this?

答案1

得分: 1

你已经从你的RoleRepositoryAppsController中导入了javax包中的Role实体。请从你的项目中导入它,它应该可以正常工作。

import 你的项目中的Role实体;
英文:

You have imported the Role entity from javax package in your RoleRepository and AppsController. Import it from your project and it should work for you.

import javax.management.relation.Role;

Change above import to your project Role entity.

答案2

得分: 1

以下是翻译好的部分:

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

  1. stereotype 注解如下:<br>
    <b>A.<i> @Component</i></b>。<br>
    <b>B.<i> @Controller</i></b>。<br>
    <b>C.<i> @Service</i></b>。<br>
    <b>D.<i> @Repository</i></b>。
    在 Spring 中必须是具体类。

  2. 最新版本的 Spring Boot 已经自动启用了 @EnableJpaRepositories,依赖于 spring-boot-starter-data-jpa

  3. @EnableJpaRepository 负责注册 CrudRepositoryJPARepository 等接口的实现。在这里,我们不需要添加 SpringFrameworkstereotype 注解。

  4. 对于接口 CrudRepositoryJPARepository,它们都与 JPA 有关,所以它们引用的类必须在具体类上具有 JPA 注解,如 @Entity@Table

现在回到您的问题陈述

对于 import javax.management.relation.Role

  1. import javax.management.relation.Role 是一个具体类,即不是抽象类 --> 已接受
  2. import javax.management.relation.Role 类没有 JPA 注解,因此我们不能直接在 CrudRepositoryJPARepository 中使用 ---> 不接受
  3. RoleRepository 不应该有 stereotype 注解。

解决方案:

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

Some basic concept on spring-boot-starter-data-jpa:

  1. stereotype annotations like: <br>
    <b>A.<i> @Component</i></b>. <br>
    <b>B.<i> @Controller</i></b> <br>
    <b>C.<i> @Service</i></b> <br>
    <b>D.<i> @Repository</i></b> <br>
    in spring must be concrete class.

  2. Latest version of Spring boot has @EnableJpaRepositories auto enabled with dependency of spring-boot-starter-data-jpa.

  3. @EnableJpaRepository is responsible of registration of interface implementation of CrudRepository, JPARepository, so on. Here we don't need to add stereotype annotations of SpringFramework.

  4. For interface CrudRepository, JPARepository has its part of JPA So class which it refer must have JPA annotations like @Entity, @Table on concrete class.

<b>Now coming back to your problem statement</b>

For import javax.management.relation.Role:

  1. import javax.management.relation.Role is a concrete class, i.e. not abstract class --> <b>Accepted</b>
  2. import javax.management.relation.Role class doesn't have JPA annotation, So we can't use directly with CrudRepository, JPARepository ---> <b>Not Accepted</b>
  3. RoleRepository should not have stereotype annotations.

<b>Solutions:</b>

  1. Create a class similar to import javax.management.relation.Role with JPA annotation on it.
  2. JPA Entity used in CrudRepository, JPARepository must have @Id field in it else you will encounter new exceptions.

答案3

得分: -1

请还要交叉检查您的实体类,我错过了下面的Id注释和主键上的生成值注释,这导致了org.springframework.beans.factory.BeanCreationException在Repository上发生。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
英文:

Please also cross check your entity class, I missed the below Id annotation, and the generated value annotation on primary key, due to which org.springframework.beans.factory.BeanCreationException occurred on the Repository.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

huangapple
  • 本文由 发表于 2020年8月13日 20:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63395358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定