Для создания не веб приложения Spring Boot нам нужно реализовать интерфейс CommandLineRunner и реализовать его метод run(String …)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package ru.leodev.examples.springboot.springbootnonwebexample; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootNonWebExampleApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(SpringBootNonWebExampleApplication.class, args); } // доступ к аргументам командной строки @Override public void run(String... args) throws Exception { // наша логика } } |
содержание
1. Структура каталогов
2. Зависимости
|
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?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>ru.leodev.examples.springboot</groupId> <artifactId>spring-boot-non-web-example</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>spring-boot-non-web-example</name> <description>Spring Boot use CommandLineRunner</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
3. Spring
3.1 Создадим сервис, который будет возвращать нам сообщение
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package ru.leodev.examples.springboot.springbootnonwebexample; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class HelloService { @Value("${name:unknown}") private String name; public String getMessage() { return getMessage(name); } public String getMessage(String name) { return "Hello " + name; } } |
|
1 |
name=Leodev |
3.2 С помощью CommandLineRunner указываем точкой входа метод run.
|
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 30 31 32 33 34 35 |
package ru.leodev.examples.springboot.springbootnonwebexample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.Banner; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import static java.lang.System.exit; @SpringBootApplication public class SpringBootNonWebExampleApplication implements CommandLineRunner { @Autowired private HelloService helloService; public static void main(String[] args) { //отключаем баннер spring boot, если не хотим видеть его лого в консоли SpringApplication app = new SpringApplication(SpringBootNonWebExampleApplication.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); } // В этом методе описываем нашу логику @Override public void run(String... args) { if (args.length > 0) { System.out.println(helloService.getMessage(args[0])); } else { System.out.println(helloService.getMessage()); } exit(0); // завершаем программу } } |
6. Запуск приложения
Собираем и запускаем проект
|
1 2 3 4 5 6 7 |
$ mvn package $ java -jar target/spring-boot-simple-1.0.jar Hello Leodev $ java -jar target/spring-boot-simple-1.0.jar "Drunk Jedi" Hello Drunk Jedi |
