A Deep Dive into Spring Boot
Tags: springboot, backend, development
Published: 2024-05-13 12:24:56

**Spring Boot** has revolutionized the way Java applications are developed and deployed. With its opinionated approach and powerful features, Spring Boot simplifies the process of creating robust and production-ready applications. In this blog post, we will take a step-by-step journey through the creation of a Spring Boot application using Maven, and we’ll explore how to deploy it into a Tomcat plugin.


Prerequisites:
Before we begin, make sure you have the following prerequisites in place:

JDK (Java Development Kit) installed on your system.
Apache Maven installed.
An IDE (Integrated Development Environment) such as IntelliJ IDEA or Eclipse.
Step 1: Create a Maven Project: To create a Spring Boot application, we’ll start by creating a Maven project. Follow these steps:

Open your preferred IDE.
Create a new Maven project and choose the appropriate Maven archetype for Spring Boot. For example, you can use the “spring-boot-starter-parent” archetype.
Define the group id, artifact id, and version for your project.
Finish creating the Maven project.
Step 2: Configure Maven Dependencies: Spring Boot provides starter dependencies that simplify the configuration of common libraries and frameworks. Let’s configure some essential dependencies:

1. Open the pom.xml file of your project.

`<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>my-spring-boot-app</artifactId>
    <version>1.0.0</version>
    <name>My Spring Boot App</name>
    <description>A sample Spring Boot application</description>
    
    <properties>
        <java.version>11</java.version>
        <spring-boot.version>2.5.3</spring-boot.version>
    </properties>
    
    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        
        <!-- Spring Web Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        
        <!-- Spring Boot Test Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            
            <!-- Spring Boot Maven Plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
</project>`

In this example, we define the basic project details such as the group id, artifact id, and version. We also set properties for the Java version and the version of Spring Boot we are using.

The dependencies section includes the necessary dependencies for our Spring Boot application. In this case, we include the spring-boot-starter for general Spring Boot functionality, spring-boot-starter-web for web-related features, and spring-boot-starter-test for testing.

The build section includes two plugins. The maven-compiler-plugin is used to configure the Java version for compilation. The spring-boot-maven-plugin is the plugin responsible for packaging our application as an executable JAR or WAR file.

Feel free to adjust the versions and add/remove dependencies according to your project’s requirements.

2. Add the necessary dependencies for Spring Boot, such as spring-boot-starter-web for web applications and spring-boot-starter-test for testing.

3. Save the pom.xml file to trigger the Maven dependencies resolution.

Step 3: Create a Spring Boot Application: In this step, we’ll create a basic Spring Boot application. Follow these steps:

Create a new Java class and name it MyApplication (or any other suitable name).
Annotate the class with @SpringBootApplication to indicate that it's a Spring Boot application. This annotation includes several other annotations, such as @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Add a main method to the class and use the SpringApplication.run method to launch the application.

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

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
```
Step 4: Build and Run the Application: Now that we have our Spring Boot application set up, let’s build and run it locally:

Use Maven to build the application. Open a terminal or command prompt, navigate to the root directory of your project, and run the following command: mvn clean install.
Once the build is successful, run the application using the following command: mvn spring-boot:run.
Your Spring Boot application should now be running on the embedded Tomcat server.
Step 5: Deploy the Application to Tomcat Plugin: To deploy our Spring Boot application to a Tomcat plugin, follow these steps:

1. Add the spring-boot-starter-tomcat dependency to the pom.xml file. This dependency includes the necessary Tomcat libraries.

```
<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>my-spring-boot-app</artifactId>
    <version>1.0.0</version>
    <name>My Spring Boot App</name>
    <description>A sample Spring Boot application</description>
    
    <properties>
        <java.version>11</java.version>
        <spring-boot.version>2.5.3</spring-boot.version>
    </properties>
    
    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        
        <!-- Spring Web Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        
        <!-- Spring Boot Test Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot.version}</version>
            <scope>test</scope>
        </dependency>
        
        <!-- Spring Boot Tomcat Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>${spring-boot.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            
            <!-- Spring Boot Maven Plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
</project>
```
2. Build the application using the mvn clean install command.

3. After a successful build, navigate to the target directory in your project and locate the generated .war file.

4. Deploy the .war file to your local or remote Tomcat server. Copy the .war file to the webapps directory of the Tomcat installation.

5. Start the Tomcat server, and your Spring Boot application will be deployed and accessible at the specified URL.

Key notes:
We explored Spring Boot and its capabilities for developing Java applications. We went through the step-by-step process of creating a Maven project, configuring dependencies, creating a Spring Boot application, and deploying it into a Tomcat plugin. Spring Boot’s simplicity and convention-over-configuration approach make it an ideal choice for developing efficient and scalable Java applications.

Remember to explore Spring Boot’s extensive documentation and experiment with various features and configurations to take full advantage of its capabilities. Happy coding with Spring Boot!