Table of Contents
In this post, we are going to create Spring Boot Hello world StandAlone application. We have already created Spring boot web hello world example.
It is very easy to configure Spring standalone project using Spring boot.
Github Source code:
Project Structure:
Step 1:  Create a simple java  project using maven in eclipse named "SpringBootHelloWorldStandaloneExample".
Step 2: Change "pom.xml" as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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>org.arpit.java2blog</groupId> <artifactId>SpringHelloWorldStandaloneExample</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project> |
Step 3: Create a file named "Country.java" in package .org.arpit.java2blog.springboot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog.springboot; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Country { @Value("{countryName}") private String countryName; public Country() { } public Country(String countryName) { super(); this.countryName = countryName; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } } |
If you notice, we are using @Value("{countryName}") for countryName attribute.Actually, this syntax is used to read a String from the property file. In the next step, we will put "countryName=India" in application.properties. We have declared this bean with @Component annotation, so spring boot will automatically find this bean using component scan.
Step 4: Create a file named "application.properties" in package src/main/resources
1 2 3 |
countryName=India |
Step 5: Create a file named "SpringBootHelloWorldStandaloneApplication.java" in package .org.arpit.java2blog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.arpit.java2blog; import org.arpit.java2blog.springboot.Country; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootHelloWorldStandaloneApplication implements CommandLineRunner { @Autowired @Qualifier("country") Country countryBean; public static void main(String[] args) { SpringApplication.run(SpringBootHelloWorldStandaloneApplication.class, args); } public void run(String... arg0) throws Exception { System.out.println("Country Name: "+countryBean.getCountryName()); } } |
You can read more about @Autowire and @Qualifier.
The SpringBootHelloWorldStandaloneApplication’s main method uses Spring Boot’s pringApplication.run() method to launch a spring application.If you notice, we did not do any single line of xml configuration here.
We have implemented CommandLineRunner interface here. This interface indicates that bean should run if it is contained in spring application.
When you run above application, you will get below output.
2017-05-06 00:12:49.914 INFO 15824 — [ main] pringBootHelloWorldStandaloneApplication : No active profile set, falling back to default profiles: default
2017-05-06 00:12:50.012 INFO 15824 — [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5579bb86: startup date [Sat May 06 00:12:50 IST 2017]; root of context hierarchy
2017-05-06 00:12:50.817 INFO 15824 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
Country Name: India
2017-05-06 00:12:50.838 INFO 15824 — [ main] pringBootHelloWorldStandaloneApplication : Started SpringBootHelloWorldStandaloneApplication in 1.421 seconds (JVM running for 2.006)
2017-05-06 00:12:50.839 INFO 15824 — [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5579bb86: startup date [Sat May 06 00:12:50 IST 2017]; root of context hierarchy
2017-05-06 00:12:50.841 INFO 15824 — [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
That’s all about Spring Boot Hello world StandAlone application.