June 21, 2022

Spring Boot Stand Alone (non web) Application Example

In the post Spring Boot Hello World Web Application we have already seen how to create a Spring Boot web application, in this post we’ll see how to write a Spring Boot stand alone (non web) application.

Also read: Create Java Project Using Maven in Eclipse to see how to create a Maven project in Eclipse.

Maven dependencies

For a stand alone application you need to add dependency for spring-boot-starter apart from the dependency for spring-boot-starter-parent.

<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.knpcode</groupId>
  <artifactId>SpringBootProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
  </parent>
  <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>

Classes for application

We’ll add a simple service to return a message.

import org.springframework.stereotype.Service;

@Service
public class HelloWorldService {
  public String getGreeting(String name) {
    return "Hello " + name;
  }
}

Application class with main method

Here is an application class with the components. For the Spring Boot non web application, Application class implements CommandLineRunner which is a functional interface. In the application class, run method of this interface has to be implemented which is the callback used to run the bean.

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 FirstSpringBootApp implements CommandLineRunner {
  @Autowired
  HelloWorldService helloWorldService;
  public static void main(String[] args) {
    SpringApplication.run(FirstSpringBootApp.class, args);
  }

  public void run(String... args) throws Exception {
    System.out.println(helloWorldService.getGreeting(args[0]));		
  }
}

@SpringBootApplication is a convenience annotation that adds all of the following annotations-

  1. @Configuration annotation tags the class as a source of bean definitions for the application context.
  2. @EnableAutoConfiguration tells Spring Boot to enable auto configuration so that beans are created automatically based on classpath settings, other beans, and various property settings. For example starter spring-boot-starter-web adds Tomcat and Spring MVC so the auto-configuration assumes that you are developing a web application and sets up Spring accordingly which includes setting up a DispatcherServlet.
  3. @ComponentScan tells Spring to look recursively for other components, configurations and services inside this package and register them.

HelloWorldService is injected automatically as a bean dependency in this class as the property is annotated with @Autowired annotation in the class.

The main method is the application entry point which delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication bootstraps this HelloWorld application, starting Spring, which, in turn, calls the run method implementation of the CommandLineRunner.

Running the application

You can run this Spring Boot Hello World application directly from IDE or by creating an executable jar. To run it directly from Eclipse IDE-

Right click FirstSpringBootApp.java – Run As – Run Configurations

There provide the program arguments for the application.

Creating executable jar

For creating a completely self-contained executable jar file run mvn package from the command line.

F:\knpcode\Spring WorkSpace\SpringBootProject>mvn package

To run application using the created jar, use the java -jar command along with an argument, as follows-

java -jar target\SpringBootProject-0.0.1-SNAPSHOT.jar knpCode

 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-28 10:36:16.686  INFO 6520 --- [           main] com.knpcode.app.FirstSpringBootApp       : No active profile set, falling back to default profiles: default
2019-07-28 10:36:18.525  INFO 6520 --- [           main] com.knpcode.app.FirstSpringBootApp       : Started FirstSpringBootApp in 3.708 seconds (JVM running for 6.762)
Hello knpCode

That's all for the topic Spring Boot Stand Alone (non web) Application Example. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment