Spring Boot is an open source framework built on top of the spring framework. “Boot” refers to the concept of rapidly initiating a spring application by handling all essentials set ups and configurations.
Core principles
- Opinionated Defaults
- Convention over configuration
- Auto-configurations
- Embedded-servers
Use of spring boot
- Web application → RESTful web services → microservices
- Batch processing → Integration with other technologies
- Testing → Monitoring and management
Spring Boot starters
- “Dependency Descriptors”
- Combines all the necessary libraries (JAR files) for a particular feature or technology into a single dependency
- Simplifies configuration
- Boosts productivity
- Ensures compatibility
Some important spring boot starters -
- spring-boot-starter
- spring-boot-starter-web
- spring-boot-starter-data-jpa
- spring-boot-starter-security
- spring-boot-starter-thymeleaf
- spring-boot-starter-test
- spring-boot-starter-actuator
- spring-boot-starter-aop
Automatically,
- Folder structure will be created
- JAR files will be added
- configurations will be done
Spring boot application
@SpringBootApplication annotation provides setup and configurations for our spring boot application
- Mix of 3 different annotations
- Configuration
- ComponentScan
- EnableAutoConfiguration
Serves as the entry point for spring boot application
SpringApplication.run() method tasks-
- Initialize spring app
- Configure ApplicationContext
- Load External configuration
- Load and register beans
- Apply auto-configuration
- Start server (if Web) etc.
In spring-boot-starter-web, the “static” folder is used to store static web resources such as HTML, CSS, javascript, images etc. The “templates” folder is used to store dynamic web pages or templates.
application.properties file acts as a config file for our spring boot application
server.port=8080
CommandLineRunner
CommandLineRunner is an interface used to execute code after the spring application context is initialized. It allows us to run additional tasks or setup logic at startup. It is used to Override the run() method (functional interfaces only have one method) It is advised to make a separate @Configuration class with all the bean objects and a CommandLineRunner bean