Saturday, August 13, 2022

Spring Boot StandAlone (Console Based) Application Example

In the post Spring Boot Hello World Web Application Example we have already seen an example of creating web application using Spring Boot. In this post we’ll see how to create a stand alone (non-web) application using Spring Boot. If you want this stand alone application to be console based where you can pass arguments to the application you can use CommandLineRunner interface.

Another option for the Spring Boot stand alone application is to get bean from the ApplicationContext the usual way.

Maven Dependencies

To see how to create a Maven project refer this post- Creating Maven Project in Eclipse

For stand alone required starter dependency is spring-boot-starter.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
  <description>Demo project for Spring Boot</description>


Service class

There is a GreetingService class that returns a greeting. This is the class that is called from the application class.

import org.springframework.stereotype.Service;

public class GreetingService {
  public String greet(String name) {
    return "Hello " + name;

Application class implementing CommandLineRunner

Here is the Application class annotated with @SpringBootApplication that bootstraps the spring boot application. For the console based Spring Boot application, Application class implements CommandLineRunner which has a run method. In the application class run method of this interface has to be implemented which is the callback used to run the bean.

In the application class GreetingService is injected as a bean dependency as the property is annotated with @Autowired annotation.

import org.netjs.SpringBootApp.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootAppApplication  implements CommandLineRunner {
  GreetingService greetingService;
  public static void main(String[] args) {, args);

  public void run(String... args) throws Exception {

Now you can run this Spring Boot application directly from Eclipse IDE by right clicking the application class and select Run As – Run Configurations. In the Run Configurations window provide Program arguments for the application.


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019-10-09 12:27:11.916  INFO 9272 --- [           main] o.n.S.SpringBootAppApplication           : Starting SpringBootAppApplication on user with PID 9272
2019-10-09 12:27:11.920  INFO 9272 --- [           main] o.n.S.SpringBootAppApplication           : No active profile set, falling back to default profiles: default
2019-10-09 12:27:13.154  INFO 9272 --- [           main] o.n.S.SpringBootAppApplication           : Started SpringBootAppApplication in 2.134 seconds 
                                                                                                   (JVM running for 3.318)
Hello netjs

Getting bean from Application Context

Another way to execute Spring Boot standalone application is to get the bean from Application Context and then call the method. Application class in that scenario looks as given below-

import org.netjs.SpringBootApp.service.GreetingService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

public class SpringBootAppApplication {
 public static void main(String[] args) {
  ApplicationContext ctx =, args);
  GreetingService greetingService = ctx.getBean(GreetingService.class);

That's all for this topic Spring Boot StandAlone (Console Based) Application Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page

Related Topics

  1. Spring Boot Spring Initializr
  2. Spring Boot - spring-boot-starter-parent
  3. Difference Between @Controller And @RestController Annotations in Spring
  4. Spring MVC Form Example With Bean Validation
  5. Lazy Initialization in Spring Using lazy-init And @Lazy Annotation

You may also like-

  1. Spring Profiles With Examples
  2. Bean Scopes in Spring With Examples
  3. Spring Expression Language (SpEL) With Examples
  4. Java Multithreading Interview Questions And Answers
  5. How to Convert Date And Time Between Different Time-Zones in Java
  6. Heap Memory Allocation in Java
  7. static Keyword in Java With Examples
  8. Constructor in Python - __init__() function