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="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>org.netjs</groupId>
  <artifactId>SpringBootApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBootApp</name>
  <description>Demo project for Spring Boot</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
  </parent>
  <properties>
    <java.version>11</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

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;

@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;

@SpringBootApplication
public class SpringBootAppApplication  implements CommandLineRunner {
  @Autowired
  GreetingService greetingService;
  public static void main(String[] args) {
    SpringApplication.run(SpringBootAppApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    System.out.println(greetingService.greet(args[0]));  
  }
}

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.

Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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;

@SpringBootApplication
public class SpringBootAppApplication {
 public static void main(String[] args) {
  ApplicationContext ctx = SpringApplication.run(SpringBootAppApplication.class, args);
  GreetingService greetingService = ctx.getBean(GreetingService.class);
  System.out.println(greetingService.greet("netjs"));
 }
}

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