How to Create a New Application with Spring Boot
- Published on
In this post I want to show you how to create a Spring Boot application and provide some information about the Spring Framework itself:
- What Is Spring Framework?
- Why Use Spring Boot?
- Bootstrapping Application with Spring Initializr
- Project Structure of Spring Boot Demo Application
- Maven as a Default Build Tool for Spring Boot Applications
- Spring Boot Application Entry Point
- Running the Spring Boot Application
- Conclusion
What Is Spring Framework?
The Spring Framework has been around almost 20 years now, and it has become the most popular framework in the Java community for building applications of all types and sizes. Spring Boot started as a subproject under Spring framework to address common tasks related to application configuration, dependency management and others. Now it is considered to be the de-facto standard for application development using Java.
Why Use Spring Boot?
The Spring Framework is packed full of features for any scenario, and with that comes a certain level of complexity in setting up a new application. This is the problem that Spring Boot set out to solve.
Spring Boot makes it easy to create standalone, production-grade, Spring-based applications. Spring Boot takes an opinionated view of the Spring platform and third-party libraries so that you can get started with minimal effort. Most Spring Boot applications need very little Spring configuration.
You could create a Spring Framework application from complete scratch, but there is no reason to. Spring Boot helps to get you up and running quickly so that you can focus on the business requirements of your application.
Spring Boot features:
- Fast bootstrapping - you specify what features you need in your application, and Spring Boot takes care of providing an actual dependencies and their versions avoid conflicts;
- Autoconfiguration - Spring Boot automatically configures the bare minimum components of a Spring application minimizing a need for manual configuration;
- Opinionated - it is expected to follow certain project structure to benefit from autoconfiguration;
- Standalone - Spring Boot applications embed a web server, so they can run standalone and do not necessarily require an external web or application server;
- Production-ready - Spring Boot provides several useful production-ready features out of the box to monitor and manage the application once it is pushed to production, such as health checks, thread dumps, and other useful metrics.
Bootstrapping Application with Spring Initializr
The fastest way to bootstrap (that's a fancy term for generate) a new Spring Boot application is by using the Spring Initalizr. The Spring Initalizr is built into popular IDEs and text editors like IntelliJ, Eclipse, and Visual Studio Code.
There is also a web interface and REST API that you can use to generate a new project.
With the Spring Initalizr, you can select attributes like:
- Dependencies: Web, DevTools, etc.
- Programming language: Java, Groovy, or Kotlin
- Java version: 17, 11, 8
- Project type: Maven, Gradle
- Packaging: packaging JAR, WAR
- Metadata: Group, Artifact, Name, Description, and Package Name
Here is how to create a new Spring Boot application with the web dependency:
curl https://start.spring.io/starter.zip -d dependencies=web -o demo.zip
Once demo.zip
is downloaded, unzip it, and you are all set.
Project Structure of Spring Boot Demo Application
The project you generated was for a Spring MVC Web project that uses Maven as the build tool, it has the following structure:
A typical project generated by Spring Initializr contains a Spring Boot application (DemoApplication
), a test, and an empty configuration. DemoApplication.java
is the main entry point for your application.
Because Spring Initializr has detected it is a web application, the static
and templates
directories have been created to hold your static resources. Also, a Maven wrapper is automatically included so that you don’t have to install Maven to run this project.
Maven as a Default Build Tool for Spring Boot Applications
The default build tool for a Spring Boot application is Maven. The pom.xml
file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want.
Dependencies
Open up the pom.xml
in the editor and locate the dependencies section:
If you've ever built a Java application for the web, you might be wondering where the rest of the dependencies are. This is because of a key feature called Spring Boot Starters that helps us manage our dependencies.
Starters are a set of convenient dependency descriptors that you can include in your application. With the starter, you get all the required dependencies along with the confidence of knowing that all the versions play nice together. For example, if you are building a web application you can include the spring-boot-starter-web
, which brings in the following:
- Spring Core
- Spring MVC
- Tomcat
- JSON Support (Jackson)
Spring Boot Maven Plugin
The Spring Boot Maven Plugin provides Spring Boot support in Apache Maven. It allows you to package executable JAR
or WAR
archives, run Spring Boot applications, generate build information, and start your Spring Boot application prior to running integration tests:
Spring Boot Application Entry Point
DemoApplication.java
has the public static void main
, which is the entry point for any Java application. In this method, the static method SpringApplication.run()
is called and passed in the current class:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The @SpringBootApplication
annotation is a convenience annotation that adds all of the following:
@SpringBootConfiguration
indicates that a class as a source of bean definitions for the application context@EnableAutoConfiguration
indicates to Spring to start adding beans based on class path settings, other beans, and various property settings@ComponentScan
indicates to Spring to look for other components, configurations, and services in the same package
Running the Spring Boot Application
Now that you know how to create a Spring Boot application and what the generated project looks like, there is one more thing you should learn - how to run the project.
If you were working in an IDE, there might be a fancy button for you to push that would run the application. What happens if you need to start it from a terminal or as part of some startup process? That is what the Spring Boot Maven plugin that you saw earlier is going to help with.
Run the application using Maven:
./mvnw spring-boot:run
Remember that you don't need Maven installed because this project is using a Maven Wrapper.
The first time you run the command you will notice a lot of dependencies being downloaded, and it might take a bit. If there are no errors, you should see the application start up, and it should be available on port 8080:
The default port of a Spring Boot application is 8080, but this is configurable.
Conclusion
As you can see, it is very easy to create a Spring application with Spring Boot due to its bootstrapping tooling, but it is only the beginning, Spring Boot has a lot more to offer.
Subscribe to RSS for new posts, I'll be posting more about Spring Framework soon.