Introducing Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. It take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
You can use Spring Boot to create Java applications that can be started by using
java -jar
or more traditional war deployments. It also provide a command line tool that runs “spring scripts”.
Our primary goals are:
- Provide a radically faster and widely accessible getting-started experience for all Spring development.
- Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
- Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).
- Absolutely no code generation and no requirement for XML configuration.
How to Build Your First Spring Boot Web Application
Prerequisites
To create a new Spring Boot application, we will use the following in our example:
You can simply download the provided links above and install them.
If you not familiar with them:
- Spring STS: The Spring Tool Suite (STS) is an Eclipse-based development environment that is customized for developing Spring applications.
- Maven: A tool that you can use for building and managing any Java-based project.
You do not need to know much about them for now, we will just use their basic functionality.
In one of my upcoming articles, we will talk about Spring STS and Maven in greater detail.
Creating Your Project
After you started your STS, right-click the package explorer and select New —> Maven project.
Then, we can use our default Workspace location.
Select the default maven-archetype-quickstart artifact.
Give any name for your Spring Boot application.
Set up Your pom.xml
After you clicked the finish button, open the pom.xml and add the spring-boot-starter-parent and the spring-boot-starter-web dependency as below:
Here, we use the Spring Boot version v 2.0.3, but you can give any valid version that you would like.
Configure Your Application as a Spring Boot Application
After that seek for your App.java class, the Maven quickstart archetype created it for us. This is where our public static void main(
String[] args
)method is located, which is the entry point of all Java applications. It should be in our root package. You will need to open it and add the @SpringBootApplication
annotation to the class and the SpringApplication.run(App.class, args)
row to our main method.Set up Your Web Application Controller
If you all have done this, create a new package called
controller
. Add a new class for this package and update the following:
The class and method name does not matter — you can call them anything.
As you can see, we added the
@
RestController
for the class, and we created a method with the @RequestMapping
annotation. These are the part of the Spring MVC framework. We will talk about that in greater detail later.Start Your Web Application
So, that's it. Now, we can start our newly created Spring Boot web application as a simple Java application:
Right click on our App.java class -> Run As -> Java Application. After this, you should see something similar in the Console log:
- 2018-08-06 20:22:53.221 INFO 9152 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
- 2018-08-06 20:22:53.225 INFO 9152 --- [ main] springboot.my_first_application.App : Started App in 1.862 seconds (JVM running for 2.419)
Here, you can see that our application has been started. Furthermore, we have a running Tomcat web server, which registered to our 8080 port.
Try Out Your Web Application
To have a look at whether our application is it working or not, we can simply open a web browser and type: http://localhost:8080/. As we saw above, 8080 was the port where our web server is registered.
You should see the following:
How Does it Work Behind the Scenes?
Dependency Management
After you added your starting dependencies, Maven downloaded all of the libraries considered to be necessary for our project.
You can check this by opening the pom.xml and clicking on the dependency hierarchy tab shown below:
It knows which dependency versions are the latest and stable for each other. This makes our life easier, because you do not need to take care of the different transitive dependencies. You only need to add one starter dependency, and your greatest dependencies start to download automatically.
The
spring-boot-starter-parent
that is added to the starter section tells the default version of our Spring Boot which one we would like to use. Thus, we do not need to give version numbers later on, because every other one is going to align like this.How Does a Spring Boot Application Start?
- Firstly, the application starts with a simple Java public static main method.
- Next, Spring starts your Spring context by looking up the auto-config initializers, configurations, and annotations that direct how to initialize and start up the Spring context.
- Lastly, it starts and auto-configures an embedded web server. The default application server is Tomcat for Spring Boot.
Benefits of Containerless Deployment
As you can see here, we didn’t need to configure our web-container. Therefore, we gain a lot of time and could get rid of some of our configuration files.
The reason that Spring Boot runs an embedded web-server and configures it itself is that it knows that we need it from the web dependency, which we added to our pom.xml.
Spring Boot opens up a bunch of new opportunities for us— we can simply run a web app by copying a basic
.jar
file anywhere Java is installed and just run it. This is a big step towards cloud architecture because we can handle our independent deployments more easily than before.
No comments:
Post a Comment