I'm trying to load beans from a different package on a Spring boot application. Here's my main class, that lives in com.xyz.app package:
Application.java:
@SpringBootApplication(scanBasePackages = { "com.xyz.app.repository" })
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args);
context.getBean(MyInterfaceRepository.class).loadData();
}
The interface MyInterfaceRepository.java is inside the com.xyz.app.repository package, and is defined as follows:
@RepositoryRestResource(collectionResourceRel = "aas", path = "aas")
public interface MyInterfaceRepository extends MongoRepository<MyClass, Long>,
MyCustomInterface {
...
}
Then, I also have a MyInterfaceRepositoryImpl.java, that lives in com.xyz.app.repository, that provides an implementation for MyCustomInterface.java, that also lives in com.xyz.app.repository.
Starting my application I get the following:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xyz.app.repository.MyInterfaceRepository] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)
at com.xyz.app.Application.main(Application.java:60)
I already checked, and indeed if I put MyInterfaceRepository.java and MyInterfaceRepositoryImpl.java in the same package as Application.java, com.xyz.app, than it works.
It seems that Spring is not able to load the beans from a different package than the one where Application.java is.
Also, I tried replacing the @SpringBootApplication with these:
@Configuration
@EnableAutoConfiguration
@ComponentScan({"com.xyz.app.repository"})
Same issue.. Any idea?