Code ví dụ Spring Boot Non Web (Java Desktop Application)

Code ví dụ Spring Boot Non Web (Java Desktop Application)

Trong bài này mình sẽ giới thiệu 3 cách thực hiện tạo project Spring Boot non-web

Cách 1: implements CommandLineRunner

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner{

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class);
    app.run(args);
  }

  @Override
  public void run(String... args) throws Exception {
    // TODO
  }
}

Cách 2: Implements ApplicationRunner

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootApplicationRunner implements ApplicationRunner{

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(SpringBootApplicationRunner.class);
    app.run(args);
  }

  @Override
  public void run(ApplicationArguments args) throws Exception {
    // TODO
  }
}

Cách 3: Sử dụng ApplicationContext

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringBootApplicationContext{

  public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(SpringBootApplicationContext.class, args);
        ctx.getBean(...);
  }

}

Với cách 1 và cách 2

  • Thường dùng để truy cập và sử dụng các agurment truyền vào lúc chạy ứng dụng, logic chương trình sẽ được viết trong method run.
  • Trong class được đánh dấu @SpringBootApplication ta vẫn có thể sử dụng @Autowired để inject các bean và sử dụng trong method run.

Với cách 3: 

  • Mọi logic đều viết trong hàm main nên không thể áp dụng @Autowired trong class @SpringBootApplication mà phải lấy các bean thông qua ApplicationContext

Code ví dụ

Tạo Spring Boot Project

spring starter project

Code ví dụ Spring Boot Non Web (Java Desktop Application)

Phần thư viện ta không cần chọn mục nào.

Code ví dụ Spring Boot Non Web (Java Desktop Application)

Cấu trúc project

Code ví dụ Spring Boot Non Web (Java Desktop Application)

File pom.xml

<?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>stackjava.com</groupId>
  <artifactId>SpringBootNonWeb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBootNonWeb</name>
  <description>Demo Spring Boot Non-Web</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.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</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>

Chỉ sử dụng spring-boot-starter

File application.properties

message=Hello World!

File HelloService.java

Đọc message từ file application.properties và in ra qua method hello()

package stackjava.com.sbnonweb.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class HelloService {

  @Autowired
  Environment env;
  
  public void hello() {
    System.out.println(env.getProperty("message"));
  }
}

File SpringBootNonWebApplication.java

Thực hiện chạy ứng dụng

package stackjava.com.sbnonweb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import stackjava.com.sbnonweb.service.HelloService;

@SpringBootApplication
public class SpringBootNonWebApplication implements CommandLineRunner{

  @Autowired
  HelloService helloService;
  
  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(SpringBootNonWebApplication.class);
    app.run(args);
  }

  @Override
  public void run(String... args) throws Exception {
    helloService.hello();
  }
}

Trường hợp không dùng CommandLineRunner

package stackjava.com.sbnonweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import stackjava.com.sbnonweb.service.HelloService;

@SpringBootApplication
public class SpringBootNonWebApplication{

  public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(SpringBootNonWebApplication2.class, args);
        HelloService helloService = ctx.getBean(HelloService.class);
        helloService.hello();
  }

}

Kết quả:

Code ví dụ Spring Boot Non Web (Java Desktop Application) stackjava.com

Code ví dụ Spring Boot Non Web (Java Desktop Application) stackjava.com

Okay, Done!

Download code ví dụ trên tại đây

 

References:

https://dzone.com/…/applicationrunner-and-commandlinerunne

https://docs.spring.io/…/getting-started-first-application.html

stackjava.com