Java > Spring Framework > Spring Boot > Creating Spring Boot Applications

Creating a Simple Spring Boot Application

This example demonstrates how to create a basic Spring Boot application with a simple REST endpoint. It showcases the fundamental components needed to get a Spring Boot application up and running quickly.

Project Setup (pom.xml)

This pom.xml file defines the project's dependencies and build configuration. Crucially, it inherits from spring-boot-starter-parent, which manages dependency versions and provides default configurations. It also includes spring-boot-starter-web for building web applications and spring-boot-starter-test for testing. The Spring Boot Maven plugin is used to package the application as an executable JAR.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>springboot-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-example</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>17</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Main Application Class

The @SpringBootApplication annotation is a convenience annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan. It tells Spring Boot to automatically configure your application based on the dependencies you've added. The SpringApplication.run() method starts the Spring application context.

package com.example.springbootexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootExampleApplication {

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

}

REST Controller

This HelloController class defines a simple REST endpoint. The @RestController annotation indicates that this class is a controller and that the methods return data directly in the response body. The @GetMapping("/hello") annotation maps HTTP GET requests to the /hello path to the hello() method, which returns the string "Hello, Spring Boot!".

package com.example.springbootexample;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }

}

Concepts Behind the Snippet

This snippet demonstrates the core principles of Spring Boot: auto-configuration, dependency management, and embedded web servers. Auto-configuration automatically configures your application based on the dependencies present in your classpath. Dependency management simplifies the process of adding and managing dependencies. The embedded web server (Tomcat, Jetty, or Undertow) allows you to run your application as a standalone executable JAR.

Real-Life Use Case

This basic structure can be expanded to create more complex REST APIs, web applications, or microservices. For example, you could add database connectivity, security features, or integration with other systems.

Best Practices

  • Use the Spring Initializr (start.spring.io) to generate your project structure.
  • Follow the Spring Boot naming conventions for your classes and packages.
  • Use properties files or environment variables for configuration.
  • Write unit tests and integration tests to ensure the quality of your code.

Interview Tip

Be prepared to explain the role of @SpringBootApplication, spring-boot-starter-parent, and the Spring Boot Maven plugin. Also, understand the concepts of auto-configuration and dependency management.

When to use them

Use Spring Boot when you need to quickly build and deploy a Java application, especially if it's a web application, REST API, or microservice. It's ideal for projects that benefit from convention-over-configuration and rapid development cycles.

Memory Footprint

Spring Boot applications generally have a larger initial memory footprint compared to traditional Spring applications due to auto-configuration and the embedded web server. However, you can optimize the memory footprint by using the appropriate dependencies, tuning the JVM settings, and using a lightweight web server like Undertow.

Alternatives

Alternatives to Spring Boot include:

  • Traditional Spring Framework: Provides more control over configuration but requires more boilerplate code.
  • Micronaut: A framework designed for building microservices with low memory footprint and fast startup times.
  • Quarkus: A Kubernetes-native Java framework designed for cloud-native applications.

Pros

  • Rapid development: Auto-configuration and convention-over-configuration speed up development.
  • Easy to deploy: Embedded web servers allow you to deploy applications as standalone JARs.
  • Large community: A large and active community provides ample support and resources.
  • Rich ecosystem: Integrates well with other Spring projects and third-party libraries.

Cons

  • Larger initial memory footprint: Can be a concern for resource-constrained environments.
  • Magic behind the scenes: Auto-configuration can sometimes make it difficult to understand what's happening under the hood.
  • Dependency management complexities: While generally helpful, dependency management can sometimes lead to conflicts.

FAQ

  • What is @SpringBootApplication?

    @SpringBootApplication is a convenience annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan. It tells Spring Boot to automatically configure your application.

  • What is spring-boot-starter-parent?

    spring-boot-starter-parent is a special POM that provides default configurations and manages dependency versions for your Spring Boot application.

  • How do I run a Spring Boot application?

    You can run a Spring Boot application by executing the main method in the main application class or by using the Spring Boot Maven plugin (mvn spring-boot:run).