Java Spring Boot
Read more at Learn the path
Definitions
- Group ID: Identifies the organization. Example: com.google
- Artifact ID: Determines the name of the application.
Compile and Run
# Auto detect changes
gradlew -t :bootJar
# Compile and run
gradlew bootRun
Packages
- Spring Boot DevTools: Provides fast application restarts, LiveReload, and configurations for enhanced development experience.
Three layers architecture
This architecture considers:
- Presentation layer (model + view + controller)
- Business logic (service): it has all the business operations (computation and decision making processes)
- Data access layer (repository): it has all the CRUD operations.
Annotations
@Component
annotation is too generic. Use@Service
for business logic layer and@Repository
for the data access layer instead. They all turn aclass
into aBean
.@Controller
also derives from@Component
.@Configuration
marks a class as a source ofBeans
definitions.@Autowired
wires the class into theBean
that needs it.@RestController
=@Controller
+@ResponseBody
.@ResponseBody
converts the output toJSON
.
Best practices
Dependency injection
- Never ever create an object inside of a class that depends on it. This is called tight coupling and makes unite testing impossible. Instead, use dependency injection.