[英]Spring boot has error creating beans for JpaRepository
i bumped into a problem with my school project i was attempting to set up h2 database for.我在尝试为其设置 h2 数据库的学校项目中遇到了问题。 Everything ran fine i guess untill i created Repository interface for my database, which looks simply like this
我想一切都运行良好,直到我为我的数据库创建了 Repository 接口,它看起来就像这样
package com.protonmail.jan.backend.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface RecordRepository extends JpaRepository {
}
When i run the application without the repository code, the app at least starts, i'm pretty new to spring-boot, so i have no idea what's going on here, but i guess it's some dependencies or something needet to create beans missing on my pom.xml file?当我在没有存储库代码的情况下运行应用程序时,应用程序至少会启动,我对 spring-boot 还是很陌生,所以我不知道这里发生了什么,但我想这是一些依赖项或需要创建我的 pom 上缺少的 bean 的东西.xml 文件? Please help, here is some of the error log i got:
请帮忙,这是我得到的一些错误日志:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
this is my Record这是我的记录
package com.protonmail.jan.backend.entity;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Record extends AbstractEntity {
@NotNull
@NotEmpty
private Date date;
@NotNull
@NotEmpty
private static Map<Integer, Room> rooms;
static {
rooms = new HashMap<Integer, Room>(){
{
put(1, new Room());
put(2, new Room());
put(3, new Room());
}
};
}
public Collection<Room> getAllRooms(){
return this.rooms.values();
}
}
this is Room, which is contained in Record这是 Room,包含在 Record 中
package com.protonmail.jan.backend.entity;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;
@Entity
public class Room extends AbstractEntity{
private String name = "pokoj";
private Boolean lit = false;
private long temp = 0;
public Room() {
}
public Boolean getLit() {
return lit;
}
public void setLit(Boolean lit) {
this.lit = lit;
}
public long getTemp() {
return temp;
}
public void setTemp(long temp) {
this.temp = temp;
}
}
this is AbstractEntity class这是 AbstractEntity class
package com.protonmail.jan.backend.entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class AbstractEntity {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Long id;
public Long getId() {
return id;
}
public boolean isPersisted() {
return id != null;
}
@Override
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AbstractEntity other = (AbstractEntity) obj;
if (getId() == null || other.getId() == null) {
return false;
}
return getId().equals(other.getId());
}
}
i also have used我也用过
public interface RecordRepository extends JpaRepository<Repository, Long> {}
Please specify the managed object and id types in interface declaration:请在接口声明中指定托管 object 和 id 类型:
public interface RecordRepository extends JpaRepository<PersitentClass, ClassId> {
}
Where: PersitentClass - the type of your entity model ClassId - the type of 'id' field其中: PersitentClass - 您的实体的类型 model ClassId - 'id' 字段的类型
For the class details, please refer to JpaRepository doc .有关 class 的详细信息,请参阅JpaRepository 文档。
@EnableJpaRepositories
annotation on the configuration class配置 class 上的
@EnableJpaRepositories
注释
https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/config/EnableJpaRepositories.html https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/config/EnableJpaRepositories.ZFC336E88832
also so @Repository
也如此
@Repository
I was stuck not being able to help spring create a bean for the custom interface that extended JpaRepository.我无法帮助 spring 为扩展 JpaRepository 的自定义接口创建一个 bean。 Tried everything, finally the answer was that Springboot expects packages to be named a certain way so that all components can be autowired.
尝试了一切,最终答案是 Springboot 期望包以某种方式命名,以便所有组件都可以自动装配。
Follow any of the common examples for Spring boot JPA CRUD, like 1遵循 Spring 引导 JPA CRUD 的任何常见示例,例如1
Usually the package part of the code is clipped.通常 package 部分代码被剪裁。 There lies the trick.
诀窍就在于此。 Spring boot expects the Application class, that is the one labelled @SpringbootApplication to be in package com.base.app, while the repository interface to be in com.base.app.repo.
Spring boot expects the Application class, that is the one labelled @SpringbootApplication to be in package com.base.app, while the repository interface to be in com.base.app.repo. If you instead have com.base.repo, it cannot locate and create the bean!!
如果您改为使用 com.base.repo,则无法找到并创建 bean!
You can solve this by adding the following to the main class where spring boot starts running:您可以通过将以下内容添加到 spring 引导开始运行的主 class 来解决此问题:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan("com.mine.alien") // path of the entity model
@EnableJpaRepositories("com.mine.demo.control") // path of jpa repository
@SpringBootApplication
public class SpringJpah2Application {
public static void main(String[] args) {
SpringApplication.run(SpringJpah2Application.class, args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.