Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scan repositories in another package

I am trying to get my Spring Boot application (with Spring Data REST) to discover Repository classes defined in another package and project. I wonder if I can configure the application to detect the Repository classes without having to rely on @EnableJpaRepositories.

For background, I have two projects. One, let's call it data project, contains Entity and Repository classes. Another, let's call it expense-tracker, is a Spring Boot application with Spring Data REST as a dependency to help to generate REST endpoints for the Repository classes in data project.

This is the structure of the projects project structure

All the Repository classes extend PagingAndSortingRepository interface. One example is below.

package com.example.data.repositories;

import com.example.data.entities.Transaction;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface TransactionRepository extends PagingAndSortingRepository<Transaction, Long> {

}

I have tried to set the scanBasePackageClasses for @SpringBootApplication (see below) but it did not work.

package com.example.expensetracker;

import com.example.data.NoOpClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication(scanBasePackageClasses = NoOpClass.class)
@EntityScan(basePackageClasses = NoOpClass.class)
public class ExpenseTrackerApplication {

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

The log message showed that Spring did not detect any Repository classes.

2019-04-17 09:33:02.465  INFO 8279 --- [           main] c.e.e.ExpenseTrackerApplication          : No active profile set, falling back to default profiles: default
2019-04-17 09:33:09.852  INFO 8279 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-04-17 09:33:10.141  INFO 8279 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 238ms. Found 0 repository interfaces.

However, when I switched to use @EnableJpaRepositories (see below), Spring detected the Repository classes.

package com.example.expensetracker;

import com.example.data.NoOpClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan(basePackageClasses = NoOpClass.class)
@EnableJpaRepositories(basePackageClasses = NoOpClass.class)
public class ExpenseTrackerApplication {

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

The log message (for the case above) indicates that Spring successfully detected the Repository classes.

2019-04-17 09:34:29.921  INFO 8370 --- [           main] c.e.e.ExpenseTrackerApplication          : No active profile set, falling back to default profiles: default
2019-04-17 09:34:35.300  INFO 8370 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-04-17 09:34:35.833  INFO 8370 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 483ms. Found 3 repository interfaces.

My expectation is that @SpringBootApplication with scanBasePackageClasses should allow Spring to detect the Repository classes. Is it not the case or I have to use another annotation, similar to EntityScan, to tell Spring Boot where to scan the Repository classes?

like image 560
Phuong Hoang Avatar asked Aug 31 '25 04:08

Phuong Hoang


1 Answers

I don't think Spring will see your repository class unless you use EnableJpaRepositories but you don't have to use this annotation in your SpringBootApplicaiton class.

You can add @ComponentScan(basePackages = {"com.example.data"}) to your SpringBootApplication class and then the following class to your data project.

package com.example.data;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@EnableAutoConfiguration
@Configuration
public class SpringConfiguration {
}

This way you increase the modularity because the expense-tracker project doesn't know what kind of configuration the data project needs. It passes the control to the data project to let it do the configuration it needs.

Hope this helps.

like image 111
mahsum Avatar answered Sep 02 '25 19:09

mahsum