Spring Boot Documentation #
Overview #
Spring Boot is an extension of the Spring Framework that simplifies the process of building production-ready applications. It provides a set of tools and conventions to streamline development and reduce boilerplate code, making it easier to create stand-alone, production-grade Spring-based applications.
Key Features #
- Auto-Configuration: Automatically configures your Spring application based on the dependencies on the classpath.
- Standalone Applications: Allows you to run your application as a standalone Java application with an embedded server.
- Production-Ready: Provides built-in features for monitoring and managing your application in production.
- Microservices: Facilitates the development of microservices with minimal configuration.
Core Concepts #
1. Auto-Configuration #
Auto-configuration attempts to automatically configure your Spring application based on the jar dependencies present in the classpath. For example, if spring-boot-starter-data-jpa
is on the classpath, it will configure JPA repositories.
Basic Example #
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
2. Starters
Starters are a set of convenient dependency descriptors you can include in your application. For example, spring-boot-starter-web includes dependencies for building web applications.
Basic Example
xml
Copy code
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. Spring Boot Initializr
Spring Boot Initializr is a web-based tool to generate a Spring Boot project with your selected dependencies. It can be accessed at start.spring.io.
Basic Usage
Go to Spring Boot Initializr.
Choose your project metadata (e.g., Group, Artifact, Name).
Select dependencies (e.g., Web, JPA, MySQL).
Click "Generate" to download a ZIP file containing your project.
4. Application Properties
Spring Boot uses application.properties or application.yml files for configuration.
Basic Example
properties
Copy code
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
yaml
Copy code
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: password
5. Embedded Servers
Spring Boot supports embedded servers like Tomcat, Jetty, and Undertow, which allows you to run your application as a standalone Java application.
Basic Example
java
Copy code
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
By default, Spring Boot uses Tomcat as the embedded server. You can switch to Jetty or Undertow by including the respective starter dependencies and excluding Tomcat.
6. Actuator
Spring Boot Actuator provides production-ready features such as health checks, metrics, and application information.
Basic Example
xml
Copy code
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
You can access various actuator endpoints, like /actuator/health and /actuator/metrics, to monitor your application.
7. Spring Boot DevTools
Spring Boot DevTools enhances the development experience with features like automatic restarts and live reload.
Basic Example
xml
Copy code
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Configuration
Java Configuration
Spring Boot applications can be configured using Java-based configuration with @Configuration and @SpringBootApplication.
java
Copy code
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
YAML Configuration
You can also use YAML for configuration.
yaml
Copy code
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: password
Profiles
Spring Boot supports different profiles for various environments (e.g., development, testing, production).
Basic Example
properties
Copy code
# application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/devdb
# application-prod.properties
spring.datasource.url=jdbc:mysql://localhost:3306/proddb
Activate a profile using:
properties
Copy code
spring.profiles.active=dev
Best Practices
Use Starters: Utilize Spring Boot starters to simplify dependency management and reduce configuration.
Profile-Based Configuration: Use profiles to manage environment-specific settings.
Externalize Configuration: Keep configuration in external files or environment variables to separate code from configuration.
Actuator Endpoints: Leverage actuator endpoints to monitor and manage your application.
Resources
Spring Boot Reference Documentation
Spring Boot Guides
Spring Initializr
Spring Boot Actuator Reference