Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a @Repository from component scan when using Spring Data Rest

in a spring boot project I have problems to exclude some repositories from the component scan.

I have a library that contains some entities and some repositories (JpaRepositories). For some reason I implemented a small Spring Boot Data Rest application that shall be used to give testers a quick access to the entities. Therefore I implemented a repository that extends the PagingAndSortingRepository and is annotated with @RepositoryRestResource.

When the application starts all repository will be scanned and made available. As long as I only want to have the Data Rest repositories available I annotated the componenten scanner to exclude the unwant repositories. But this doesn't work. I checked with the actuator beans endpoint and whatever I do - no repositories are excluded.

To demonstrate the problem I created a simple demo application: https://github.com/magomi/springboot-restdata-repoloading.

To exclude the DataRepository I tried the two approaches:

// exclude V02
@SpringBootApplication
@ComponentScan(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
                DataRepository.class})
})

and

// exclude V01
@SpringBootApplication(exclude = { DataRepository.class })

Without success. When I call the /beans endpoint (provided by spring boot actuator) I always see

{
    bean: "dataRepository",
    aliases: [ ],
    scope: "singleton",
    type: "org.codefromhell.test.repoloading.DataRepository",
    ...
},
{
    bean: "dataApiRepository",
    aliases: [ ],
    scope: "singleton",
    type: "org.codefromhell.test.repoloading.api.DataApiRepository",
    ...
},
like image 852
magomi Avatar asked Dec 08 '16 18:12

magomi


People also ask

How do you exclude certain interfaces from instantiation as repository?

To exclude an interface extending Repository from being instantiated as a repository instance it can either be annotate it with @NoRepositoryBean or moved out side of the configured base-package .

Is @repository annotation mandatory?

It is indeed not necessary to put the @Repository annotation on interfaces that extend JpaRepository ; Spring recognizes the repositories by the fact that they extend one of the predefined Repository interfaces. From the javadoc: Annotation to enable JPA repositories.

How do I exclude a configuration class in spring boot?

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring. autoconfigure. exclude property.

Can we exclude any package without using then basePackages filter Yes No?

Yes. It is possible to exclude package without using the basePackages filter by simply using the exclude attribute while using the @SpringBootApplication annotation.


1 Answers

Because it's a repository and not strictly a @Component, you need to excluded it by adding @EnableJpaRepositories to your application:

@SpringBootApplication
@EnableJpaRepositories(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
                DataRepository.class})
})
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}
like image 61
Strelok Avatar answered Oct 07 '22 01:10

Strelok