Spring Boot 非web应用程序实例
来源:互联网 发布:人工智能开发语言 编辑:程序博客网 时间:2024/05/16 15:30
在Spring Boot中,要创建一个非Web应用程序,实现CommandLineRunner
并覆盖run()
方法,例如:
import org.springframework.boot.CommandLineRunner;@SpringBootApplicationpublic class SpringBootConsoleApplication implements CommandLineRunner { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootConsoleApplication.class, args); } //access command line arguments @Override public void run(String... args) throws Exception { //do something }}
1. 项目依赖
只有依赖 spring-boot-starter
库,参考如下 pom.xml
-
<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.yiibai</groupId> <artifactId>spring-boot-non-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-non-web</name> <url>http://maven.apache.org</url> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
2. Spring
返回消息的服务,如下 HelloMessageService.java 代码所示 -
package com.yiibai.service;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;@Servicepublic class HelloMessageService { @Value("${name:unknown}") private String name; public String getMessage() { return getMessage(name); } public String getMessage(String name) { return "Hello " + name; }}
属性文件配置文件: application.properties 如下所示 -
name=yiibai
下面是CommandLineRunner
示例,如果运行这个Spring Boot,那么run
方法将是入口点。
SpringBootConsoleApplication.java 代码内容如下所示 -
package com.yiibai;import com.yiibai.service.HelloMessageService;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;@SpringBootApplicationpublic class SpringBootConsoleApplication implements CommandLineRunner { @Autowired private HelloMessageService helloService; public static void main(String[] args) throws Exception { //disabled banner, don't want to see the spring logo SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); } // Put your logic here. @Override public void run(String... args) throws Exception { if (args.length > 0) { System.out.println(helloService.getMessage(args[0].toString())); } else { System.out.println(helloService.getMessage()); } exit(0); }}
3. 实例运行演示
打包上面的项目并运行它,如下命令 -
## Go to project directory## package it$ mvn package$ java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jarHello yiibai$ java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar "Max su"Hello Max su
阅读全文
0 0
- Spring Boot 非web应用程序实例
- spring boot启动时调用自己的非web逻辑
- 4. Spring Boot 创建简单的web应用程序
- Hello Spring Boot应用程序
- Spring Boot应用程序启动器
- 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案
- Spring boot 入门 实例
- Spring Boot AOP实例
- spring-boot 实例
- Spring Boot devtools实例
- Spring Boot入门实例
- Spring boot 入门实例
- spring-boot入门实例
- spring boot入门实例
- Spring Boot @ConfigurationPropreties 实例
- spring boot web部署
- spring boot web
- Spring Boot Web
- php curl_multi批量发送http请求
- mysql导入过大.sql文件报错处理
- 标准BT.656并行数据结构
- Linux/Centos 安装Redis遇到问题及解决
- C字符串操作总结
- Spring Boot 非web应用程序实例
- QML Image: Cannot open: qrc:/image/1.png
- 一周 Go World 新鲜事[Asta 优选]
- Mysql学习-基础笔记
- 翻车事故分析专栏
- 基于SSM的汽车租赁系统
- 仿人机器人的跑步研究学习笔记1之机器人的基础知识
- 非旋转 Treap 学习笔记(一)
- 让 ul 里面的li居中