Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Spring Boot

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Spring Boot Tutorial

Spring Boot Tutorial provides basic and advanced concepts of Spring Framework.
Our Spring Boot Tutorial is designed for beginners and professionals both.

Spring Boot is a Spring module that provides the RAD (Rapid Application
Development) feature to the Spring framework.

Our Spring Boot Tutorial includes all topics of Spring Boot such, as features, project,
maven project, starter project wizard, Spring Initializr, CLI, applications, annotations,
dependency management, properties, starters, Actuator, JPA, JDBC, etc.

What is Spring Boot


Spring Boot is a project that is built on the top of the Spring Framework. It provides
an easier and faster way to set up, configure, and run both simple and web-based
applications.

It is a Spring module that provides the RAD (Rapid Application


Development) feature to the Spring Framework. It is used to create a stand-alone
Spring-based application that you can just run because it needs minimal Spring
configuration.

In short, Spring Boot is the combination of Spring Framework and Embedded


Servers.
In Spring Boot, there is no requirement for XML configuration (deployment
descriptor). It uses convention over configuration software design paradigm that
means it decreases the effort of the developer.

We can use Spring STS IDE or Spring Initializr to develop Spring Boot Java
applications.

Why should we use Spring Boot Framework?

We should use Spring Boot Framework because:

ADVERTISEMENT
ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT

o The dependency injection approach is used in Spring Boot.


o It contains powerful database transaction management capabilities.
o It simplifies integration with other Java frameworks like JPA/Hibernate ORM,
Struts, etc.
o It reduces the cost and development time of the application.

Along with the Spring Boot Framework, many other Spring sister projects help to
build applications addressing modern business needs. There are the following Spring
sister projects are as follows:

o Spring Data: It simplifies data access from the relational


and NoSQL databases.
o Spring Batch: It provides powerful batch processing.
o Spring Security: It is a security framework that provides robust security to
applications.
o Spring Social: It supports integration with social networking like LinkedIn.
o Spring Integration: It is an implementation of Enterprise Integration Patterns.
It facilitates integration with other enterprise applications using lightweight
messaging and declarative adapters.

Advantages of Spring Boot


o It creates stand-alone Spring applications that can be started using Java -jar.
o It tests web applications easily with the help of different Embedded HTTP
servers such as Tomcat, Jetty, etc. We don't need to deploy WAR files.
o It provides opinionated 'starter' POMs to simplify our Maven configuration.
o It provides production-ready features such as metrics, health
checks, and externalized configuration.
o There is no requirement for XML configuration.
o It offers a CLI tool for developing and testing the Spring Boot application.
o It offers the number of plug-ins.
o It also minimizes writing multiple boilerplate codes (the code that has to be
included in many places with little or no alteration), XML configuration, and
annotations.
o It increases productivity and reduces development time.

Limitations of Spring Boot


Spring Boot can use dependencies that are not going to be used in the application.
These dependencies increase the size of the application.

Goals of Spring Boot


The main goal of Spring Boot is to reduce development, unit test, and integration
test time.

o Provides Opinionated Development approach


o Avoids defining more Annotation Configuration
o Avoids writing lots of import statements
o Avoids XML Configuration.

By providing or avoiding the above points, Spring Boot Framework


reduces Development time, Developer Effort, and increases productivity.

ADVERTISEMENT

Prerequisite of Spring Boot


To create a Spring Boot application, following are the prerequisites. In this tutorial,
we will use Spring Tool Suite (STS) IDE.

o Java 1.8
o Maven 3.0+
o Spring Framework 5.0.0.BUILD-SNAPSHOT
o An IDE (Spring Tool Suite) is recommended.

Spring Boot Features


o Web Development
o SpringApplication
o Application events and listeners
o Admin features
o Externalized Configuration
o Properties Files
o YAML Support
o Type-safe Configuration
o Logging
o Security

Web Development

It is a well-suited Spring module for web application development. We can easily


create a self-contained HTTP application that uses embedded servers like Tomcat,
Jetty, or Undertow. We can use the spring-boot-starter-web module to start and
run the application quickly.

ADVERTISEMENT
ADVERTISEMENT

SpringApplication

The SpringApplication is a class that provides a convenient way to bootstrap a Spring


application. It can be started from the main method. We can call the application just
by calling a static run() method.

1. public static void main(String[] args)


2. {
3. SpringApplication.run(ClassName.class, args);
4. }

Application Events and Listeners

ADVERTISEMENT
ADVERTISEMENT

Spring Boot uses events to handle the variety of tasks. It allows us to create factories
file that is used to add listeners. We can refer it to using the ApplicationListener
key.

Always create factories file in META-INF folder like META-INF/spring.factories.

ADVERTISEMENT
ADVERTISEMENT
Admin Support

Spring Boot provides the facility to enable admin-related features for the application.
It is used to access and manage applications remotely. We can enable it in the Spring
Boot application by using spring.application.admin.enabled property.

Externalized Configuration

Spring Boot allows us to externalize our configuration so that we can work with the
same application in different environments. The application uses YAML files to
externalize configuration.

Properties Files

Spring Boot provides a rich set of Application Properties. So, we can use that in the
properties file of our project. The properties file is used to set properties like server-
port =8082 and many others. It helps to organize application properties.

ADVERTISEMENT
ADVERTISEMENT

YAML Support

It provides a convenient way of specifying the hierarchical configuration. It is a


superset of JSON. The SpringApplication class automatically supports YAML. It is an
alternative of properties file.

Type-safe Configuration

The strong type-safe configuration is provided to govern and validate the


configuration of the application. Application configuration is always a crucial task
which should be type-safe. We can also use annotation provided by this library.

Logging

Spring Boot uses Common logging for all internal logging. Logging dependencies
are managed by default. We should not change logging dependencies if no
customization is needed.

Security

ADVERTISEMENT
ADVERTISEMENT

Spring Boot applications are spring bases web applications. So, it is secure by default
with basic authentication on all HTTP endpoints. A rich set of Endpoints is available
to develop a secure Spring Boot application.

ADVERTISEMENT
Prerequisite
Before learning Spring Boot, you must have the basic knowledge of Spring
Framework.

ADVERTISEMENT

Audience
Our Spring Boot Tutorial is designed to help beginners and professionals.

Problem
We assure you that you will not find any problem with the Spring Boot Tutorial. But if
there is any mistake, please post the problem in the contact form.

Next Topic Spring Boot Version

Spring Boot Version


The latest version of Spring Boot is 2.0. It introduces a lot of new features along with
some modifications and replacement.

Spring Boot 2.0


Let's have a sneak peek at Spring Boot 2.0.

ADVERTISEMENT
ADVERTISEMENT

o What's New
Infrastructure Upgrade
Spring Framework 5
o What's Changed
Configuration Properties
Gradle Plugin
Actuators endpoints
o What's Evolving
Security
Metrics
The pivotal team has upgraded the infrastructure in which the following tools are
involved:

o Supports Java 8 or above versions


o Supports Apache Tomcat 8 or above versions
o Supports Thymeleaf 3
o Supports Hibernate 5.2

In Spring Framework 5, the Pivotal team upgraded the following:

o Reactive Spring
i. Servlet stack

a. Servlet Container
b. Servlet API
c. Spring MVC
ii. Reactive Stack

a. Netty, Servlet 3.1, Undertow


b. Reactive HTTP Layer
c. Spring WebFlux
o Functional API
o Kotlin Support

The latest version of Spring Boot is 2.2.1. This release of Spring Boot includes 110
fixes, dependency upgrades, and improvements.

In the Spring Boot v2.2.1, the annotation @ConfigurationProperties scanning is


now disabled by default. We need to be explicitly opted into by adding
the @ConfigurationPropertiesScan annotation.

New Features
o Support constructor binding for property nested inside a JavaBean
o Add config property for CodecConfigurer.maxInMemorySize in WebFlux
o Make test slices' type exclude filters public
o Support amqps:// URIs in spring.rabbitmq.addresses

Dependency upgrades
Some dependencies have been upgraded in Spring Boot v2.2.1 are as follows:
o Mongodb 3.11.2
o Spring Security 5.2.1.RELEASE
o Slf4j 1.7.29
o Spring Hateoas 1.0.1.RELEASE
o Hibernate Validator 6.0.18.Final
o Hibernate 5.4.8.Final
o Jetty 9.4.22.v20191022
o Spring Framework 5.2.1
o Spring AMQP 2.2.1
o H2 1.4.200
o Spring Security 5.2
o Spring Batch 4.2

Some important and widely used third-party dependencies are upgraded in this
release are as follows:

o Micrometer 1.3.1
o Flyway 6.0.7
o Elasticsearch 6.8.4
o JUnit 5.5
o Jackson 2.10

Performance Improvements
In Spring Boot 2.2.1 the following performance has been improved:

Lazy Initialization

ADVERTISEMENT
ADVERTISEMENT

In Spring Boot 2.2.1, we can enable global lazy initialization by using the
property spring.main.lazy-initialization property. It reduces the application startup
time.

Java 13 Support

Spring Boot 2.2.1 now supports the latest version of Java that is Java 13.

Immutable Binding

In the newer version of Spring Boot, Configuration properties support constructor-


based binding. The class annotates with @ConfigurationProperties annotation is to
be immutable. It can be enabled by adding an
annotation @ConfugurationProperties to a class or one of its constructors
with @ConstructorBinding.

RSocket Support

It is a part of Spring Security. RSocket integration is auto-configured when an


application finds spring-security-rsocket is present on the classpath.

ADVERTISEMENT
ADVERTISEMENT

ADVERTISEMENT

Deprecations in Spring Boot 2.2


o The property logging.file has renamed to logging.file.name.
o The property logging.path has renamed to logging.file.path.
o The server.connection-timeout property has been deprecated in favor of
server-specific properties.
o Joda time support is deprecated in favor of java.time.

The following improvements are made in the Spring Boot 2.2.1

o Java: Spring Boot 2.2.1


o Spring Framework 5.2: This release of Spring Boot upgrades to Spring
Framework to 5.2.
o JMX is disabled: In this version, JMX is not enabled by default. We can enable
it by using the property jmx.enabled=true. If you are using the IDE feature to
monitor your application, we need to enable it.
o Fork enabled by default: Spring Boot application that ran by Maven Plugin is
now forked by default.
o JUnit 5: Spring Boot v2.2.1 provides JUnit 5 by default. JUnit 5's vintage
engine is also included by default that supports existing JUnit 4-based test
classes. We can also use JUnit 4 and JUnit 5 based test classes in the same
module.
o AssertJ 3.12: This release of Spring Boot upgrades to AssertJ 3.12. It contains
a breaking API changes for assertions related to Iterator.
o Hibernate Dialect: In the newer version of Spring Boot, Hibernate chose the
dialect to use rather than applying a default dialect based on the detected
database.
o Gradle Requirements: The latest version of Spring Boot requires Gradle 4.10.
Spring vs. Spring Boot vs. Spring
MVC
Spring vs. Spring Boot
Spring: Spring Framework is the most popular application development framework
of Java. The main feature of the Spring Framework is dependency
Injection or Inversion of Control (IoC). With the help of Spring Framework, we can
develop a loosely coupled application. It is better to use if application type or
characteristics are purely defined.

Spring Boot: Spring Boot is a module of Spring Framework. It allows us to build a


stand-alone application with minimal or zero configurations. It is better to use if we
want to develop a simple Spring-based application or RESTful services.

The primary comparison between Spring and Spring Boot are discussed below:

Spring Spring Boot

Spring Framework is a widely Spring Boot Framework is widely used to


used Java EE framework for develop REST APIs.
building applications.

It aims to simplify Java EE It aims to shorten the code length and provide the
development that makes easiest way to develop Web Applications.
developers more productive.

The primary feature of the Spring The primary feature of Spring Boot
Framework is dependency is Autoconfiguration. It automatically configures
injection. the classes based on the requirement.

It helps to make things simpler It helps to create a stand-alone application with


by allowing us to less configuration.
develop loosely
coupled applications.

The developer writes a lot of It reduces boilerplate code.


code (boilerplate code) to do
the minimal task.

To test the Spring project, we Spring Boot offers embedded server such
need to set up the sever as Jetty and Tomcat, etc.
explicitly.

It does not provide support for It offers several plugins for working with an
an in-memory database. embedded and in-memory database such as H2.

Developers manually define Spring Boot comes with the concept of starter in
dependencies for the Spring pom.xml file that internally takes care of
project in pom.xml. downloading the dependencies JARs based on
Spring Boot Requirement.

Spring Boot vs. Spring MVC


Spring Boot: Spring Boot makes it easy to quickly bootstrap and start developing a
Spring-based application. It avoids a lot of boilerplate code. It hides a lot of
complexity behind the scene so that the developer can quickly get started and
develop Spring-based applications easily.

Spring MVC: Spring MVC is a Web MVC Framework for building web applications. It
contains a lot of configuration files for various capabilities. It is an HTTP oriented web
application development framework.

Spring Boot and Spring MVC exist for different purposes. The primary comparison
between Spring Boot and Spring MVC are discussed below:

Spring Boot Spring MVC

Spring Boot is a module of Spring for Spring MVC is a model view


packaging the Spring-based application with controller-based web framework
sensible defaults. under the Spring framework.

It provides default configurations to It provides ready to use features for


build Spring-powered framework. building a web application.

There is no need to build configuration It requires build configuration


manually. manually.

There is no requirement for a deployment A Deployment descriptor is required.


descriptor.

It avoids boilerplate code and wraps It specifies each dependency


dependencies together in a single unit. separately.
It reduces development time and increases It takes more time to achieve the
productivity. same.

Spring Boot Architecture


Spring Boot is a module of the Spring Framework. It is used to create stand-alone,
production-grade Spring Based Applications with minimum efforts. It is developed
on top of the core Spring Framework.

Spring Boot follows a layered architecture in which each layer communicates with the
layer directly below or above (hierarchical structure) it.

Before understanding the Spring Boot Architecture, we must know the different
layers and classes present in it. There are four layers in Spring Boot are as follows:

ADVERTISEMENT
ADVERTISEMENT

o Presentation Layer
o Business Layer
o Persistence Layer
o Database Layer
Presentation Layer: The presentation layer handles the HTTP requests, translates the
JSON parameter to object, and authenticates the request and transfer it to the
business layer. In short, it consists of views i.e., frontend part.

Business Layer: The business layer handles all the business logic. It consists of
service classes and uses services provided by data access layers. It also
performs authorization and validation.

Persistence Layer: The persistence layer contains all the storage logic and translates
business objects from and to database rows.

Database Layer: In the database layer, CRUD (create, retrieve, update, delete)
operations are performed.

Spring Boot Flow Architecture

o Now we have validator classes, view classes, and utility classes.


o Spring Boot uses all the modules of Spring-like Spring MVC, Spring Data, etc.
The architecture of Spring Boot is the same as the architecture of Spring MVC,
except one thing: there is no need for DAO and DAOImpl classes in Spring
boot.
o Creates a data access layer and performs CRUD operation.
o The client makes the HTTP requests (PUT or GET).
o The request goes to the controller, and the controller maps that request and
handles it. After that, it calls the service logic if required.
o In the service layer, all the business logic performs. It performs the logic on
the data that is mapped to JPA with model classes.
o A JSP page is returned to the user if no error occurred.

Spring Initializr
Spring Initializr is a web-based tool provided by the Pivotal Web Service. With the
help of Spring Initializr, we can easily generate the structure of the Spring Boot
Project. It offers extensible API for creating JVM-based projects.

It also provides various options for the project that are expressed in a metadata
model. The metadata model allows us to configure the list of dependencies
supported by JVM and platform versions, etc. It serves its metadata in a well-known
that provides necessary assistance to third-party clients.

Spring Initializr Modules


Spring Initializr has the following module:

ADVERTISEMENT
ADVERTISEMENT

o initializr-actuator: It provides additional information and statistics on project


generation. It is an optional module.
o initializr-bom: In this module, BOM stands for Bill Of Materials. In Spring
Boot, BOM is a special kind of POM that is used to control the versions of a
project's dependencies. It provides a central place to define and update those
versions. It provides flexibility to add a dependency in our module without
worrying about the versions.
Outside the software world, the BOM is a list of parts, items, assemblies, and
other materials required to create products. It explains what,
how, and where to collect required materials.
o initializr-docs: It provides documentation.
o initializr-generator: It is a core project generation library.
o initializr-generator-spring:
o initializr-generator-test: It provides a test infrastructure for project
generation.
o initializr-metadata: It provides metadata infrastructure for various aspects of
the projects.
o initializr-service-example: It provides custom instances.
o initializr-version-resolver: It is an optional module to extract version
numbers from an arbitrary POM.
o initializr-web: It provides web endpoints for third party clients.

Supported Interface
o It supports IDE STS, IntelliJ IDEA Ultimate, NetBeans, Eclipse. You can
download the plugin from https://github.com/AlexFalappa/nb-springboot. If
you are using VSCode, download the plugin
from https://github.com/microsoft/vscode-spring-initializr.
o Use Custom Web UI http://start.spring.io or https://start-scs.cfapps.io.
o It also supports the command-line with the Spring Boot
CLI or cURL or HTTPie.

The following image shows the Spring Initializr UI:


Generating a Project
Before creating a project, we must be friendly with UI. Spring Initializr UI has the
following labels:

o Project: It defines the kind of project. We can create either Maven


Project or Gradle Project. We will create a Maven Project throughout the
tutorial.
o Language: Spring Initializr provides the choice among three languages Java,
Kotlin, and Groovy. Java is by default selected.
o Spring Boot: We can select the Spring Boot version. The latest version
is 2.2.2.
o Project Metadata: It contains information related to the project, such
as Group, Artifact, etc. Group denotes the package name; Artifact denotes
the Application name. The default Group name is com.example, and the
default Artifact name is demo.
o Dependencies: Dependencies are the collection of artifacts that we can add
to our project.

There is another Options section that contains the following fields:

o Name: It is the same as Artifact.


o Description: In the description field, we can write a description of the
project.
o Package Name: It is also similar to the Group name.
o Packaging: We can select the packing of the project. We can choose
either Jar or War.
o Java: We can select the JVM version which we want to use. We will use Java
8 version throughout the tutorial.

There is a Generate button. When we click on the button, it starts packing the
project and downloads the Jar or War file, which you have selected.

Download and Install STS IDE


Spring Tool Suite (STS) IDE
Spring Tool Suite is an IDE to develop Spring applications. It is an Eclipse-based
development environment. It provides a ready-to-use environment to implement,
run, deploy, and debug the application. It validates our application and provides
quick fixes for the applications.
Installing STS
Step 1: Download Spring Tool Suite from https://spring.io/tools3/sts/all. Click on the
platform which you are using. In this tutorial, we are using the Windows platform.

Step 2: Extract the zip file and install the STS.

sts-bundle -> sts-3.9.9.RELEASE -> Double-click on the STS.exe.

Step 3: Spring Tool Suite 3 Launcher dialog box appears on the screen. Click on
the Launch button. You can change the Workspace if you want.

Step 4: It starts launching the STS.


The STS user interface looks like the following:

ADVERTISEMENT

Creating a Spring Boot Project


Following are the steps to create a simple Spring Boot Project.

Step 1: Open the Spring initializr https://start.spring.io.

Step 2: Provide the Group and Artifact name. We have provided Group
name com.javatpoint and Artifact spring-boot-example.

Step 3: Now click on the Generate button.


When we click on the Generate button, it starts packing the project in a .rar file and
downloads the project.

Step 4: Extract the RAR file.

Step 5: Import the folder.

File -> Import -> Existing Maven Project -> Next -> Browse -> Select the project ->
Finish

It takes some time to import the project. When the project imports successfully, we
can see the project directory in the Package Explorer. The following image shows
the project directory:
SpringBootExampleApplication.java

ADVERTISEMENT
ADVERTISEMENT

1. package com.javatpoint.springbootexample;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBootApplication;
4. @SpringBootApplication
5. public class SpringBootExampleApplication
6. {
7. public static void main(String[] args)
8. {
9. SpringApplication.run(SpringBootExampleApplication.class, args);
10. }
11. }

pom.xml

1. <?xml version="1.0" encoding="UTF-8"?>


2. <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <parent>
5. <groupId>org.springframework.boot</groupId>
6. <artifactId>spring-boot-starter-parent</artifactId>
7. <version>2.2.2.BUILD-SNAPSHOT</version>
8. <relativePath/> <!-- lookup parent from repository -->
9. </parent>
10. <groupId>com.javatpoint</groupId>
11. <artifactId>spring-boot-example</artifactId>
12. <version>0.0.1-SNAPSHOT</version>
13. <name>spring-boot-example</name>
14. <description>Demo project for Spring Boot</description>
15. <properties>
16. <java.version>1.8</java.version>
17. </properties>
18. <dependencies>
19. <dependency>
20. <groupId>org.springframework.boot</groupId>
21. <artifactId>spring-boot-starter</artifactId>
22. </dependency>
23. <dependency>
24. <groupId>org.springframework.boot</groupId>
25. <artifactId>spring-boot-starter-test</artifactId>
26. <scope>test</scope>
27. <exclusions>
28. <exclusion>
29. <groupId>org.junit.vintage</groupId>
30. <artifactId>junit-vintage-engine</artifactId>
31. </exclusion>
32. </exclusions>
33. </dependency>
34. </dependencies>
35. <build>
36. <plugins>
37. <plugin>
38. <groupId>org.springframework.boot</groupId>
39. <artifactId>spring-boot-maven-plugin</artifactId>
40. </plugin>
41. </plugins>
42. </build>
43. <repositories>
44. <repository>
45. <id>spring-milestones</id>
46. <name>Spring Milestones</name>
47. <url>https://repo.spring.io/milestone</url>
48. </repository>
49. <repository>
50. <id>spring-snapshots</id>
51. <name>Spring Snapshots</name>
52. <url>https://repo.spring.io/snapshot</url>
53. <snapshots>
54. <enabled>true</enabled>
55. </snapshots>
56. </repository>
57. </repositories>
58. <pluginRepositories>
59. <pluginRepository>
60. <id>spring-milestones</id>
61. <name>Spring Milestones</name>
62. <url>https://repo.spring.io/milestone</url>
63. </pluginRepository>
64. <pluginRepository>
65. <id>spring-snapshots</id>
66. <name>Spring Snapshots</name>
67. <url>https://repo.spring.io/snapshot</url>
68. <snapshots>
69. <enabled>true</enabled>
70. </snapshots>
71. </pluginRepository>
72. </pluginRepositories>
73. </project>

Step 6: Run the SpringBootExampleApplication.java file.

Right-click on the file -> Run As -> Java Applications

The following image shows the application runs successfully.

Download Project

reating a Spring Boot Project Using


STS
We can also use Spring Tool Suite to create a Spring project. In this section, we will
create a Maven Project using STS.

Step 1: Open the Spring Tool Suite.

Step 2: Click on the File menu -> New -> Maven Project

It shows the New Maven Project wizard. Click on the Next button.

Step 3: Select the maven-archetype-quickstart and click on the Next button.

Step 4: Provide the Group Id and Artifact Id. We have provided Group
Id com.javatpoint and Artifact Id spring-boot-example-sts. Now click on
the Finish button.
When we click on the Finish button, it creates the project directory, as shown in the
following image.

Step 5: Open the App.java file. We found the following code that is by default.

App.java

1. package com.javatpoint;
2. public class App
3. {
4. public static void main( String[] args )
5. {
6. System.out.println( "Hello World!" );
7. }
8. }
The Maven project has a pom.xml file which contains the following default
configuration.

ADVERTISEMENT

ADVERTISEMENT

pom.xml

1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/


2001/XMLSchema-instance"
2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/
maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <groupId>com.javatpoint</groupId>
5. <artifactId>spring-boot-example-sts</artifactId>
6. <version>0.0.1-SNAPSHOT</version>
7. <packaging>jar</packaging>
8. <name>spring-boot-example-sts</name>
9. <url>http://maven.apache.org</url>
10. <properties>
11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12. </properties>
13. <dependencies>
14. <dependency>
15. <groupId>junit</groupId>
16. <artifactId>junit</artifactId>
17. <version>3.8.1</version>
18. <scope>test</scope>
19. </dependency>
20. </dependencies>
21. </project>

Step 6: Add Java version inside the <properties> tag.

1. <java.version>1.8</java.version>

Step 7: In order to make a Spring Boot Project, we need to configure it. So, we are
adding spring boot starter parent dependency in pom.xml file. Parent is used to
declare that our project is a child to this parent project.

1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-parent</artifactId>
4. <version>2.2.1.RELEASE</version>
5. <type>pom</type>
6. </dependency>

Step 8: Add the spring-boot-starter-web dependency in pom.xml file.

1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-web</artifactId>
4. <version>2.2.1.RELEASE</version>
5. </dependency>
Note: When we add the dependencies in the pom file, it downloads the related jar
file. We can see the downloaded jar files in the Maven Dependencies folder of the
project directory.

ADVERTISEMENT

After adding all the dependencies, the pom.xml file looks like the following:

pom.xml

1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/


2001/XMLSchema-instance"
2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/
maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <groupId>com.javatpoint</groupId>
5. <artifactId>spring-boot-example-sts</artifactId>
6. <version>0.0.1-SNAPSHOT</version>
7. <packaging>jar</packaging>
8. <name>spring-boot-example-sts</name>
9. <url>http://maven.apache.org</url>
10. <properties>
11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12. <java.version>1.8</java.version>
13. </properties>
14. <dependencies>
15. <dependency>
16. <groupId>org.springframework.boot</groupId>
17. <artifactId>spring-boot-starter-parent</artifactId>
18. <version>2.2.1.RELEASE</version>
19. <type>pom</type>
20. </dependency>
21. <dependency>
22. <groupId>org.springframework.boot</groupId>
23. <artifactId>spring-boot-starter-web</artifactId>
24. <version>2.2.1.RELEASE</version>
25. </dependency>
26. <dependency>
27. <groupId>junit</groupId>
28. <artifactId>junit</artifactId>
29. <version>3.8.1</version>
30. <scope>test</scope>
31. </dependency>
32. </dependencies>
33. </project>

Step 9: Create a class with the name SpringBootExampleSts in the


package com.javatpoint.

ADVERTISEMENT

ADVERTISEMENT

Right-click on the package name -> New -> Class -> provide the class name ->
Finish
Step 10: After creating the class file, call the static method run() of the
SpringApplication class. In the following code, we are calling the run() method and
passing the class name as an argument.

1. SpringApplication.run(SpringBootExampleSts.class, args);

Step 11: Annotate the class by adding an annotation @SpringBootApplication.

ADVERTISEMENT

ADVERTISEMENT

@SpringBootApplication

A single @SpringBootApplication annotation is used to enable the following


annotations:

ADVERTISEMENT

ADVERTISEMENT
o @EnableAutoConfiguration: It enables the Spring Boot auto-configuration
mechanism.
o @ComponentScan: It scans the package where the application is located.
o @Configuration: It allows us to register extra beans in the context or import
additional configuration classes.

ADVERTISEMENT
ADVERTISEMENT

SpringBootApplicationSts.java

1. package com.javatpoint;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBootApplication;
4. @SpringBootApplication
5. public class SpringBootExampleSts
6. {
7. public static void main(String[] args)
8. {
9. SpringApplication.run(SpringBootExampleSts.class, args);
10. }
11. }

Step: Run the file SpringBootExampleSts.java, as Java Application. It displays the


following in the console.

The line Started SpringBootExampleSts in 5.038 seconds (JVM running for


6.854) in the console shows that the application is up and running.

You might also like