在使用spring boot 构建应用启动时,我们在工作中都是通过命令行来启动应用,有时候会需要一些特定的参数以在应用启动时,做一些初始化的操作。
spring boot 提供了 commandlinerunner 和 applicationrunner 这两个接口供用户使用。
1. commandlinerunner
1.1 声明:
|
1
2
3
4
5
6
7
8
9
10
11
|
@functionalinterface
public interface commandlinerunner {
/**
* callback used to run the bean.
* @param args incoming main method arguments
* @throws exception on error
*/
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
|
package com.example.consoleapplication;
import org.springframework.boot.commandlinerunner;
import org.springframework.stereotype.component;
@component
public class testrunner implements commandlinerunner {
@override
public void run(string... args) {
// do something...
for(string arg: args){
system.out.println(arg);
}
system.out.print("test command runner");
}
}
|
1.3 运行结果
运行: java -jar build/libs/consoleapplication-0.0.1-snapshot.jar -sdfsaf sdfas,
结果如下:
2019-03-16 17:31:56.544 info 18679 — [ main] c.e.consoleapplication.demoapplication : no active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195 info 18679 — [ main] c.e.consoleapplication.demoapplication : started demoapplication in 16.172 seconds (jvm running for 16.65)
-sdfsaf
sdfas
test command runner%
2. applicationrunner
2.1 声明
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* interface used to indicate that a bean should <em>run</em> when it is contained within
* a {@link springapplication}. multiple {@link applicationrunner} beans can be defined
* within the same application context and can be ordered using the {@link ordered}
* interface or {@link order @order} annotation.
*
* @author phillip webb
* @since 1.3.0
* @see commandlinerunner
*/
@functionalinterface
public interface applicationrunner {
/**
* callback used to run the bean.
* @param args incoming application arguments
* @throws exception on error
*/
void run(applicationarguments args) throws exception;
}
|
2.2 使用
applicationrunner 和 commandlinerunner 的使用是有差别的:
- commandlinerunner 的使用,只是把参数根据空格分割。
-
applicationrunner 会根据 是否匹配 –key=value 来解析参数,
- 能匹配,则为 optional 参数, 可用getoptionvalues获取参数值。
- 不匹配则是 non optional 参数。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.example.consoleapplication;
import org.springframework.boot.applicationrunner;
import org.springframework.stereotype.component;
import org.springframework.boot.applicationarguments;
@component
public class testapplicationrunner implements applicationrunner {
@override
public void run(applicationarguments args) throws exception {
// do something...
system.out.println("option arg names" + args.getoptionnames());
system.out.println("non option+" + args.getnonoptionargs());
}
}
|
2.3 运行结果
运行命令 java -jar build/libs/consoleapplication-0.0.1-snapshot.jar -non1 non2 --option=1, 结果为:
2019-03-16 18:08:08.528 info 19778 — [ main] c.e.consoleapplication.demoapplication : no active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166 info 19778 — [ main] c.e.consoleapplication.demoapplication : started demoapplication in 16.059 seconds (jvm running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
–option=1
test%
可以看到, optional 参数名有 option, non optional 参数有 -non1 和 non2
3. 小结
commandlinerunner 和 applicationrunner 都能实现命令行应用启动时根据参数获取我们需要的值,做特殊的逻辑。但两者有所不同,推荐使用 applicationrunner 的 optional 参数, 方便扩展。
4. 参考文档
https://docs.spring.io/spring-boot/docs/2.0.5.release/reference/htmlsingle/#boot-features-web-environment
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://segmentfault.com/a/1190000018530137
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 104
-
2025-05-27 35
-
2025-05-27 15
-
2025-05-29 21
-
2025-05-27 27

