I am learning spring boot. This is my spring boot project structure. I do intentionally keep my application.java in a different package to learn about @ComponentScan
Project source - https://github.com/kitkars/spring-boot
Error :
The application failed to start due to below error. *************************** APPLICATION FAILED TO START *************************** Description: Field productRepository in com.test.service.ProductService required a bean of type 'com.test.repository.ProductRepository' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.test.repository.ProductRepository' in your configuration. Process finished with exit code 1
This is my Application.java
@SpringBootApplication
@ComponentScan(basePackages = "com.test")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Now If I move my Application.java
under com.test
, everything works just great.
What If my Application.java is not under com.test
- can it not use the ComponentScan
packages and start from there? All the controller, services, repositories etc are present under com.test
.