Spring Boot

Rapid application development with auto-configuration

🚀 What is Spring Boot?

Spring Boot simplifies Spring application development with auto-configuration, embedded servers, and production-ready features. It eliminates boilerplate configuration and helps you create stand-alone applications quickly.


// Complete Spring Boot application in one file
@SpringBootApplication
@RestController
public class HelloApplication {
    
    @GetMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
    
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}
                                    

Spring Boot Features

âš¡

Auto-Configuration

Automatically configures Spring based on dependencies

@SpringBootApplication
// Includes @Configuration, @EnableAutoConfiguration
📦

Embedded Server

Built-in Tomcat, Jetty, or Undertow

// No need for external server
java -jar myapp.jar
🔧

Starter Dependencies

Pre-configured dependency bundles

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
📊

Actuator

Production-ready monitoring and management

// Health check endpoint
GET /actuator/health

🔹 Creating a Spring Boot Application

Build a complete REST API in minutes:

@SpringBootApplication
public class BookstoreApplication {
    public static void main(String[] args) {
        SpringApplication.run(BookstoreApplication.class, args);
    }
}

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    
    // Constructors, getters, setters
}

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
    List<Book> findByAuthor(String author);
}

@RestController
@RequestMapping("/api/books")
public class BookController {
    
    @Autowired
    private BookRepository bookRepository;
    
    @GetMapping
    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }
    
    @PostMapping
    public Book createBook(@RequestBody Book book) {
        return bookRepository.save(book);
    }
}

Output:

GET /api/books → Returns all books

POST /api/books → Creates new book

🔹 Application Properties

Configure your application using application.properties:

# Server configuration
server.port=8080
server.servlet.context-path=/api

# Database configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop

# Logging configuration
logging.level.com.example=DEBUG
logging.file.name=app.log

# Custom properties
app.name=My Spring Boot App
app.version=1.0.0

🔸 Using Properties in Code

@Component
public class AppInfo {
    
    @Value("${app.name}")
    private String appName;
    
    @Value("${app.version}")
    private String appVersion;
    
    public String getInfo() {
        return appName + " v" + appVersion;
    }
}

🔹 Common Starter Dependencies

Spring Boot provides many starter dependencies:

Popular Starters:

  • spring-boot-starter-web: Web applications with REST
  • spring-boot-starter-data-jpa: JPA with Hibernate
  • spring-boot-starter-security: Security features
  • spring-boot-starter-test: Testing framework
  • spring-boot-starter-actuator: Monitoring endpoints
<!-- Maven dependencies -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

🔹 Running Spring Boot Applications

Multiple ways to run your Spring Boot app:

# Using Maven
mvn spring-boot:run

# Using Gradle
./gradlew bootRun

# As JAR file
java -jar target/myapp-1.0.0.jar

# With profiles
java -jar myapp.jar --spring.profiles.active=prod

Console Output:

Started Application in 2.345 seconds

Tomcat started on port(s): 8080 (http)

🧠 Test Your Knowledge

What does @SpringBootApplication annotation include?