In this tutorial we will create a spring boot sample application which will print the text “Hello World”. This tutorial is to just give an introduction on how Spring Boot works right out of the box with zero configuration.
After importing the code to STS as mentioned in the previous example, open the .java file. By default the java file will have the following code.
package com.codetreat.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Main Class
Type the following code inside the main method to print “Hello World” text as shown below.
package com.codetreat.codetreat; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CodetreatApplication { public static void main(String[] args) { SpringApplication.run(CodetreatApplication.class, args); System.out.println("Hello World"); } }
POM.XML File
The pom.xml file must look as shown below. There is no need to make any changes to the default pom.xml file.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.codetreat</groupId> <artifactId>codetreat</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>codetreat</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
The maven dependencies specified in the pom.xml fill will be downloaded when the java file is saved. The progress of the download will be displayed as shown below.
Running the Spring Boot Sample Application
Click the green play button displayed on the tool bar at the top of the IDE as shown in the below image and select the Run As -> Spring Boot App option to execute the program.
The text “Hello World” will be printed in the end of the execution as shown below.
As you can see, it is so easy to start writing the business logic right out of the box using Spring Boot with minimal to zero configuration. The developers of Spring Boot project had the struggles of numerous java developers in mind and developed a fantastic module to make the lives of Java developers easy. Such features were available for quite sometime in frameworks of other languages such as Django of Python and Rails of Ruby. However, in Java, Spring Framework which is the de facto standard of developing modern enterprise apps, was not having such project till the arrival of Spring Boot.
In the next tutorials we will look in detail about various features of the Spring Boot, annotations, developing standalone web app, building a REST API etc.
PreviousNext