How to Create and Bootstrap a Simple Boot Application?
- To create any simple spring boot application, we need to start by creating a
pom.xml
file. Add spring-boot-starter-parent
as parent of the project which makes it a spring boot application.pom.xml<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId><artifactId>myproject</artifactId><version>0.0.1-SNAPSHOT</version>
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.1.RELEASE</version></parent></project>
Above is simplest spring boot application which can be packaged as jar file. Now import the project in your IDE (optional). - Now we can start adding other starter dependencies that we are likely to need when developing a specific type of application. For example, for a web application, we add a spring-boot-starter-web dependency.pom.xml
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
- Start adding application business logic, views and domain data at this step. By default, Maven compiles sources from
src/main/java
so create your application code inside this folder only.As the very initial package, add the application bootstrap class and add @SpringBootApplication annotation. This class will be used to run the application. It’s main()
method acts as the application entry point.MyApplication.javaimport
org.springframework.boot.SpringApplication;import
org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic
class
MyApplication {public
static
void
main(String[] args) {SpringApplication.run(MyApplication.class, args);}}
- The SpringApplication class provided in above
main()
method bootstraps our application, starting Spring, which, in turn, starts the auto-configured Tomcat web server. MyApplication
(method argument) indicates the primary Spring component. - Since we used the spring-boot-starter-parent POM, we have a useful “run” goal that we can use to start the application. Type
'mvn spring-boot:run'
from the root project directory to start the application.It will start the application which we can verify in console logs as well as in browser by hitting URL: localhost:8080
.
Post Views: 133
Tags: Spring BootSpring Boot AnnotationsSpring Boot Configuration
Yaniv Levy
Yaniv Levy, Entrepreneur, visioner & technology passionate with over 20 years on vast experience as a Senior Software Engineer and a Software Architect.
You may also like...