1

Yes, Spring Data JPA is a nice feature, that delegates the creation/implementation of a database query to Springboot. I was able to use this simply by creation of a configuration class, that was annotated with @Configuration and @EnableJpaRepositories.

Now want to put all that DB related code in a maven-module and want to access this function from outside. In order to archive this i added a @Import(MyConfiguration.class) to my @SpringBootApplication class.

Unfortunately i am not able to access my repo, because i receive the following exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'de.demo.dbdomain.repo.MyRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1812) ~[spring-beans-6.0.5.jar:6.0.5]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1371) ~[spring-beans-6.0.5.jar:6.0.5]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325) ~[spring-beans-6.0.5.jar:6.0.5]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:709) ~[spring-beans-6.0.5.jar:6.0.5]
        ... 106 common frames omitted

I guess i need to initialize the code inside my module, but i don not know how.

I hope to get the initialisation of springJPA done with my @Import.

Groovieman
  • 31
  • 5

2 Answers2

0

Assuming that the JPA module is also a Spring Boot application, you can import the @SpringBootApplication main class from the module in the other application.

grekier
  • 2,322
  • 1
  • 16
  • 23
  • The JPA-module was a SpringBoot application, now it is a library, that will be accessed by many other application modules. Should it not be sufficient, to import the @EnableJpaRepositories Configuration-class? – Groovieman Mar 07 '23 at 13:48
  • It might be if it is a configuration class and all classes you need are in sub packages. That is why I usually use the main application class as there is no overhead and you always have the correct configuration from there. – grekier Mar 08 '23 at 08:20
  • This is a good point, maybe it would be a good idea, to leave a main-class in the project, that accesses all required interfaces. – Groovieman Mar 08 '23 at 09:06
0

You also need to declare the package where spring-boot needs to scan for candidate repositories.

So together with @SpringBootApplication you also need the:

@EnableJpaRepositories(basePackages = {"package1", "package2"}) so that spring boot can look in the specific path of that module and register required repositories.

In case you also have entities in this other module with @Entity you will also need the @EntityScan(basePackages = {"yourpackage3", "yourpackage4}).

By default if those packages are not provided spring boot will look for spring annotations and repositories only in the package where the annotation @SpringBootApplication exists or deeper nested packages from the package where the annotation @SpringBootApplication exists.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • Thank you for your help. I was able to find a bug in my code. The Package to scan was already done in my annotated Configuration with the em.setPackageToScan. I assume, that @EntityScan and setPackagesToScan are equivalent, or not. – Groovieman Mar 07 '23 at 15:04
  • @Groovieman did you fix the issue? em.setPackageToScan should be ok for entities to be identified but for spring repositories you still need to indicate the package with `@EnableJpaRepositories(basePackages = {"package1", "package2"})` – Panagiotis Bougioukos Mar 07 '23 at 15:12
  • No, unfortunately not. I tried the \@EntityScan in the application module and also the setPackageToScan API inside the Configuration/EnableJpaRepositories Class. There is one more problem, the \@Entity classes resides in another jar. – Groovieman Mar 07 '23 at 16:04
  • @Groovieman then you need both annotations I have in my answer, try to provide both in your main `@SpringBootApplication` class. – Panagiotis Bougioukos Mar 07 '23 at 16:09
  • Yes you may be right, the `@EntityScan` inside the running application (module) seems to work better than the `@Configuration @EnableJpaRepositories DataSourceConfiguration` class with its annotated entityManagerFactoryRef method, that performs a em.em.setPackaegToScan. It seems to me, that the setPackageToScan only run reliable on Java-classes in the same module. I'll give your a short notice, when i try to solve that issue in 2h (need a pause) – Groovieman Mar 07 '23 at 16:19
  • I have to redraw my previously made assumption, `@EntityScan` and `em.setPackaegToScan` are doing the same thing. This is a good result, because the call behaviour in a referenced module behaves in the same way as outside (here `@EntityScan` in the exe-module of the caller *vs* `em.setPackageToScan` inside a library module. – Groovieman Mar 08 '23 at 11:12
  • @Groovieman I have written this multiple times however you still mention the same. @EntityScan is used to scan for entities. Ok you have it with em.setPackageToScan. You still need SPRING FRAMEWORK to know where to look for spring repositories. Therefore you need the `@EnableJpaRepositories(basePackages = {"package1", "package2"})` – Panagiotis Bougioukos Mar 08 '23 at 11:16
  • Sorry for my late response. I started the project as a real SpringBootJPA project using spring-initializr. I had to integrate an existing JPA persitence model with lot of `@Entity`classes, that are *partly* polluted with non-JPA classes and interfaces (for instance web-interfaces). I guess, that the problem may be unresolved artifacts in the JPA related classes. As a consequence of this, i started to clean up code from my colleagues. Due to the amount of entity-classes i hope to get it fixed today. – Groovieman Mar 09 '23 at 12:25
  • Great, let's talk about compatibility in the field of modern Java development: The linked library uses the import-path `javax.persistence` SpingBoot-3 uses `jakarta.persistence`. – Groovieman Mar 09 '23 at 16:37