I have a custom spring-boot-starter that attempts to autoconfigure some JPA Repositories so that a consuming service can utilise them, for example...
@Repository
public interface LibraryRepository extends JpaRepository<LibraryEntity, Long> { }
@Slf4j
@AutoConfiguration
@Import({ LibraryService.class })
@ImportAutoConfiguration(classes = JpaRepositoriesAutoConfiguration.class)
@EntityScan(basePackages = { LibraryEntityMarker.ENTITY_MARKER })
public class LibraryAutoConfiguration {
public LibraryAutoConfiguration() {
log.info( "Loaded autoconfiguration" );
}
}
This seems to work fine until the service using the library has its own entities & repositories, then I typically see something akin to...
Parameter 0 of constructor in example.service.MyService required a bean of type 'example.Repository.MyRepository' that could not be found.
I seem to be able to fix this by doing the following...
@SpringBootApplication
@EnableJpaRepositories(basePackages = { MyRepositoryMarker.REPO_MARKER,
LibraryRepositoryMarker.REPO_MARKER })
@EntityScan(basePackageClasses = MyEntity.class)
public class MyApplication {
public static void main( String[] args ) {
SpringApplication.run( MyApplication.class, args );
}
Is there a way I can avoid having to use @EnableJpaRepositories & @EntityScan?
Ideally I'd like to be able to add the starter dependency and everything just works with no code changes required to the consuming service, e.g.
@SpringBootApplication
public class MyApplication {
public static void main( String[] args ) {
SpringApplication.run( MyApplication.class, args );
// Service starts and all repos & entities are automatically configured for use.
}
(Spring Boot 3.2.1)