简体   繁体   English

Spring Boot启用注释被忽略

[英]Spring boot Enable annotation ignored

I'm beginning at Sprint Boot 5 and I'm facing somewhat of an issue that I don't understand. 我从Sprint Boot 5开始,但遇到了一些我不了解的问题。 Maybe can somebody shed some light on that for me. 也许有人可以为我阐明这一点。

I'm using Sprint Boot 2.1.7 with Spring JPA and PostgreSQL. 我将Sprint Boot 2.1.7与Spring JPA和PostgreSQL一起使用。 I'm trying to simply enable the discovery of JpaRepositories. 我试图简单地启用JpaRepositories的发现。 When I have the @EnableJpaRepositories on my "main" class (the one with the @SprintBootApplication annotation), everything works fine. 当我有@EnableJpaRepositories对我的“主”类(一个与@SprintBootApplication注释),一切工作正常。 However, I want to pick up good practices, so I want to split my configuration between concerns. 但是,我想学习一些好的做法,所以我想将配置分为两个关注点。 In that effect, I created a JpaConfig class to register beans and enable JPA features for my app. 为此,我创建了一个JpaConfig类来注册bean并为我的应用启用JPA功能。 However, the enable annotations seem to be ignored. 但是,启用注释似乎被忽略了。

I tried to register a dummy Bean in this config file to make sure component scanning found my class, and it works correctly. 我试图在此配置文件中注册一个虚拟Bean,以确保组件扫描找到了我的类,并且它可以正常工作。 I've seen example of enable attributes on configuration classes online. 我已经在线看到了配置类上的启用属性的示例。 Why is it not working for me? 为什么对我不起作用? Am I missing something? 我想念什么吗? Is it something that was disabled in recent versions of Spring Boot? 是在最新版本的Spring Boot中禁用的功能吗?

Here is what my code looks like: 这是我的代码:

src/main/java/com/gretro/petclinic/PetClinicApplication.java src / main / java / com / gretro / petclinic / PetClinicApplication.java

package com.gretro.petclinic;

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

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

src/main/java/com/gretro/petclinic/config/JpaConfig.java src / main / java / com / gretro / petclinic / config / JpaConfig.java

package com.gretro.petclinic.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaConfig {
}

src/main/java/com/gretro/petclinic/vets/repositories/VetSpecialtiesRepository.java src / main / java / com / gretro / petclinic / vets / repositories / VetSpecialtiesRepository.java

package com.gretro.petclinic.vets.repositories;

import com.gretro.petclinic.vets.models.VetSpecialty;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface VetSpecialtiesRepository extends JpaRepository<VetSpecialty, Long> {
}

Here is the error I get at boot: 这是我在启动时遇到的错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.gretro.petclinic.init.DataSeeder required a bean of type 'com.gretro.petclinic.vets.repositories.VetSpecialtiesRepository' that could not be found.


Action:

Consider defining a bean of type 'com.gretro.petclinic.vets.repositories.VetSpecialtiesRepository' in your configuration.

Specify the package to scan 指定要扫描的包

@EnableJpaRepositories(basePackages = "com.gretro.petclinic.vets.repositories")

Annotation to enable JPA repositories. 启用JPA存储库的注释。 Will scan the package of the annotated configuration class for Spring Data repositories by default. 默认情况下,将扫描带注释的配置类的包中的Spring Data存储库。

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/config/EnableJpaRepositories.html https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/config/EnableJpaRepositories.html

@EnableJpaRepositories @EnableJpaRepositories

For instance, Enabling auto-configuration support for Spring Data JPA required to know the path of the JPA the repositories. 例如,启用对Spring Data JPA的自动配置支持需要了解JPA存储库的路径。 By default, it will scan only the main application package and its sub-packages for detecting the JPA repositories. 默认情况下,它将仅扫描主应用程序包及其子包以检测JPA存储库。 Therefore, if the JPA repositories are placed under the main application package or its subpackage, then it will be detected by the @EnableAutoConfiguration as a part of auto-configuring the spring-based configurations. 因此,如果将JPA存储库放置在主应用程序包或其子包下,则@EnableAutoConfiguration会将其检测为自动配置基于Spring的配置的一部分。 If the repository classes are not placed under the main application package or its subpackage, then the relevant repository package(s) should be declared in the main application configuration class with @EnableJpaRepositories annotation. 如果存储库类未放置在主应用程序包或其子包下,则应在主应用程序配置类中使用@EnableJpaRepositories批注声明相关存储库包。 Then this will enable the JPA repositories contains in the given/declared package(s). 然后,这将启用给定/声明的包中包含的JPA存储库。

Annotation to enable JPA repositories. 启用JPA存储库的注释。 Will scan the package of the annotated configuration class for Spring Data repositories by default. 默认情况下,将扫描带注释的配置类的包中的Spring Data存储库。

e.g.
    @EnableJpaRepositories(basePackages = "com.springbootdev.examples.jpa.repositories")

This description will help you to understand more about this annotation. 此描述将帮助您了解有关此注释的更多信息。

Since you're using Spring Boot, you don't need to mark any @Configuration class with the @EnableJpaRepositories . 由于您使用的是Spring Boot,因此无需使用@EnableJpaRepositories标记任何@Configuration类。 Spring Boot's auto configuration do the job. Spring Boot的自动配置可以完成这项工作。

You can safely remove this annotation. 您可以安全地删除此注释。

The @EnableJpaRepositories will tell Spring Boot that you want to take control over the Spring Data JPA Repositories configuration. @EnableJpaRepositories会告诉Spring Boot您想控制Spring Data JPA Repositories配置。

If this is your case, you'll need to specify the packages to be scanned for your repositories: 如果是这种情况,则需要指定要扫描其存储库的软件包:

@EnableJpaRepositories(basePackages = {"com.gretro.petclinic.vets.repositories"})

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

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