SpringBoot ComponentScan issue with multi-module project

I have a myaven parent pom project of type maven with myapp-core and myapp-web modules. The myapp-core module is added as a dependency on myapp network.

All classes in the myapp-core module are in the root package com.myapp.core , and all classes in the myapp-web module are in the root package com.myapp.web

The main Application.java is also in the com.myapp.web package. Since my main module of the root module is different, I include the general base package "com.myapp" for ComponentScan as follows:

@Configuration @ComponentScan(basePackages="com.myapp") @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

Now it’s amazing that if I run this application using Start As β†’ Spring Boot App , it works fine. But if I ran it as Run as> Java application , he made a mistake with the error saying that he could not find the beans defined in the myapp-core module.

If I transfer the Application.java package to com.myapp , it works fine. It should work even if I run it as a Java application, right?

+8
source share
3 answers

After turning on the debug logging level for spring and going through extensive logs, I found that scanning for various components, such as JPA repositories, JPA objects, etc., depends on the name of the Application.java package.

If the repositories or JPA entities are not in subpackages of the Application.java package, then we must specify them explicitly as follows:

 @Configuration @ComponentScan(basePackages="com.sivalabs.jcart") @EnableAutoConfiguration @EnableJpaRepositories(basePackages="com.sivalabs.jcart") @EntityScan(basePackages="com.sivalabs.jcart") public class Application{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

With the above optional @EnableJpaRepositories , @EntityScan above, I can run it using Run as> Java Application .

But still not sure how it works fine when run as β†’ spring Boot application !!

In any case, I think it's better to port the Application.java package to com.myapp , and not fight SpringBoot!

+16
source

I have the same problem. Only adding the @EnableJpaRepositories annotation can solve the problem. I tried defining basePackages in @SpringBootApplication, but to no avail. I think the Application class package is loaded into the JpaRepositories scan process, but other packages defined in @SpringBootApplication are ignored. It looks like a bug / improvement to Spring Boot.

0
source

I had a similar problem with Redis repositories, which was fixed in a similar way:

 @Configuration @EnableConfigurationProperties({RedisProperties.class}) @RequiredArgsConstructor @EnableRedisRepositories(basePackages = {"com.example.another"}) public class RedisConfig { private final RedisConnectionFactory redisConnectionFactory; @Bean public RedisTemplate<?, ?> redisTemplate() { RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>(); template.setConnectionFactory(redisConnectionFactory); template.afterPropertiesSet(); return template; } } 
0
source

All Articles