Reputation: 12405
I have a myapp parent pom type maven project with myapp-core and myapp-web modules. myapp-core module is added as dependency to myapp-web.
All the classes in myapp-core module reside in root package com.myapp.core and all classes in myapp-web module reside in root package com.myapp.web
The main Application.java is also in com.myapp.web package. As my core module root package is different I am including common 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 the surprising thing is if I run this app using Run As -> Spring Boot App it is working fine. But if I run it as Run As -> Java Application it is failing with error saying it can't found beans defined in myapp-core module.
If I move my Application.java
to com.myapp
package it is working fine.
It should work even if i run it as Java Application also, right?
Upvotes: 15
Views: 23541
Reputation: 3708
I had a similar issue with Redis repositories that 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;
}
}
Upvotes: 0
Reputation: 183
I have the same problem. Only adding the @EnableJpaRepositories annotation can solve the issue. I tried to define basePackages in @SpringBootApplication, to no avail. I think the package of the Application class is fed to the scanning process of JpaRepositories, but other packages defined in @SpringBootApplication are ignored. It looks like a bug/improvement of Spring Boot.
Upvotes: 0
Reputation: 12405
After enabling debug log level for spring and going through extensive logs I found that scanning for various components like JPA Repositories, JPA Entities etc are depending on the Application.java's package name.
If the JPA Repositories or Entities are not in sub packages of Application.java
's package then we need to 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 additional @EnableJpaRepositories
, @EntityScan
I am able to run it using Run As -> Java Application.
But still not sure how it is working fine when Run As -> Spring Boot App!!
Anyway I think it is better to move my Application.java
to com.myapp
package rather than fighting with SpringBoot!
Upvotes: 25