简体   繁体   English

Spring 引导实体和存储库在没有注释的情况下无法工作

[英]Spring Boot Entity and Repository Not Working Without Annotation

I read this stackoverflow post which says that we don't need to use @EnableJpaRepository or @EntityScan Spring data repository works without annotations我读了这篇 stackoverflow 帖子,上面说我们不需要使用@EnableJpaRepository@EntityScan Spring 数据存储库无需注释即可工作

I'm not sure what's wrong.我不确定出了什么问题。 But my Spring Boot application is only able to start with @EnableJpaRepository and @ComponentScan .但是我的 Spring 引导应用程序只能以@EnableJpaRepository@ComponentScan启动。

MyApplication.java MyApplication.java

package com.domain.spring.example;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan("com.domain.spring.example.repository")
@EnableJpaRepositories(
    basePackages = "com.domain.spring.example.model",
    entityManagerFactoryRef = "entityManagerFactory"
)
public class MyApplication {

  public static void main(String[] args) {
    new SpringApplicationBuilder(MyApplication.class).run(args);
  }
}

Item.java Item.java

package com.domain.spring.example.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.math.BigDecimal;
import java.util.UUID;

import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@Table(name = "items")
public class Item {

  @Id
  @GeneratedValue(generator = "UUID")
  @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
  private UUID id;

  private String name;

  private BigDecimal price;
}

ItemRepository.java项目Repository.java

package com.domain.spring.example.respository;

import com.domain.spring.example.model.Item;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ItemRepository extends JpaRepository<Item, UUID> {

}

ItemService.java项目Service.java

package com.domain.spring.example.service;

import com.domain.spring.example.respository.ItemRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ItemService {

  private final ItemRepository itemRepository;


}

build.gradle build.gradle

plugins {
    id 'java'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'org.springframework.boot' version '2.6.4'
}

group 'com.domain'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

configurations {
    testIntegrationImplementation.extendsFrom implementation
    testIntegrationRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {

    annotationProcessor group: 'org.hibernate', name: 'hibernate-jpamodelgen'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
    testAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.22'

    implementation group: 'com.vladmihalcea', name: 'hibernate-types-52', version: '2.14.0'
    implementation group: 'jakarta.persistence', name: 'jakarta.persistence-api', version: '3.1.0-RC2'
    implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
    implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
    implementation group: 'org.hibernate', name: 'hibernate-core'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
    implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '3.1.1'
//    implementation group: 'org.springframework.data', name: 'spring-data-jpa'
    implementation group: 'org.springframework.data', name: 'spring-data-redis'
    implementation group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
    implementation group: 'org.postgresql', name: 'postgresql'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'

    testIntegrationImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

application.properties应用程序属性

# Eureka
eureka.client.enabled=false
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:4001/eureka/
eureka.instance.prefer-ip-address=true

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost/example
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.check_nullability=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

# Logging
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler=OFF
logging.level.org.springframework.web.HttpLogging=OFF

Without the @EnableJpaRepositories annotation in MyApplication.java .MyApplication.java中没有@EnableJpaRepositories注释。 I get the following error我收到以下错误

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.domain.spring.example.model.Item

Without the @ComponenctScan annotation in MyApplication.java .MyApplication.java中没有@ComponenctScan注释。 I get the following error我收到以下错误

Parameter 0 of constructor in com.domain.spring.example.service.ItemService required a bean of type 'com.domain.spring.example.respository.ItemRepository' that could not be found.

In addition, the ddl-auto=create property doesn't work as well.此外, ddl-auto=create属性也不起作用。 How to fix it so that I can use the default Spring JPA without adding annotations?如何修复它以便我可以使用默认的 Spring JPA 而不添加注释?

Update : I think there can be any problem somewhere that prevents Spring to correctly scan repositories, entities, and also routes.更新:我认为某处可能存在任何问题阻止 Spring 正确扫描存储库、实体和路由。 This simple route returns 404 when I hit GET /v1/test当我点击 GET /v1/test时,这条简单的路线返回 404

package com.domain.spring.example.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/v1")
@RequiredArgsConstructor
public class MyController {

  @GetMapping("/test")
  public Mono<String> test() {
    return Mono.just("test");
  }
}

You are looking for repositories in this package您正在此 package 中寻找存储库

@EnableJpaRepositories(
    basePackages = "com.domain.spring.example.model"

but your repository is in this package但是您的存储库在此 package

com.domain.spring.example.respository

Two things here:这里有两件事:

@EnableJpaRepositories(
    basePackages = "com.domain.spring.example.respository",
    entityManagerFactoryRef = "entityManagerFactory"
)

also since you're using SpringBoot Application, it's auto wiring should take care of instantiating repositories provided the class annotated with @SpringBootApplication is in a top level package, which you have, so I think you should try without the @EnableJpaRepositories too.此外,由于您使用的是 SpringBoot 应用程序,因此它的自动连接应该负责实例化存储库,前提是使用 @SpringBootApplication 注释的@SpringBootApplication位于您拥有的顶级 package 中,所以我认为您也应该尝试不使用@EnableJpaRepositories

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM