简体   繁体   中英

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. Maybe can somebody shed some light on that for me.

I'm using Sprint Boot 2.1.7 with Spring JPA and PostgreSQL. I'm trying to simply enable the discovery of JpaRepositories. When I have the @EnableJpaRepositories on my "main" class (the one with the @SprintBootApplication annotation), everything works fine. 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. 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. 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?

Here is what my code looks like:

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

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

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. Will scan the package of the annotated configuration class for Spring Data repositories by default.

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

@EnableJpaRepositories

For instance, Enabling auto-configuration support for Spring Data JPA required to know the path of the JPA the repositories. By default, it will scan only the main application package and its sub-packages for detecting the JPA repositories. 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. 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. Then this will enable the JPA repositories contains in the given/declared package(s).

Annotation to enable JPA repositories. Will scan the package of the annotated configuration class for Spring Data repositories by default.

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's auto configuration do the job.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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