Spring Boot - Code Structure
Recommended Spring Boot Project code structure.
Typical Layout
A typical spring boot project layout:
1 | com |
It is recommended that the Application class that contains the main method to be located at the root package above other classes. This allows package scan to apply on your project. Application class is usually annotated with @SpringBootApplication annotation.
Application.java
1 | package com.example.myapplication; |
@SpringBootApplication Annotation
@SpringBootApplication is a composite of three annotations
- @EnableAutoConfiguration: attempts to automatically configure Spring application based on dependencies
- @ComponentScan: allows spring beans for the application to be scanned
- @Configuration: enables java base configuration
Dependency Injection Makes Easy
If you follow the typical Spring Boot Application structure, there is no need to use @ComponentScan to scan beans. @SpringBootApplication at the root package will scan all beans in the application. Just use @Autowired to wire the beans
Reference