代码改变世界

spring boot创建非web项目

  youxin  阅读(72)  评论(0编辑  收藏  举报

我们如何启动一个main方法去运行它呢

使用也非常简单,我们只需要对springboot生成的代码做略微的修改即可。 使用SpringApplicationBuilder来创建SpringApplication,并且配置WebApplicationType为NONE,这样即使有tomcat依赖也不会创建http server, 执行run方法之后我们就得到了spring里的ApplicationContext,通过ApplicationContext.getBean能够拿到我们的任意一个bean,得到bean之后再调用我们想调用的方法,而不需要启动http server再用http接口去触发调用

 

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(DemoApplication.class).web(WebApplicationType.NONE).run(args);
        applicationContext.getBean(HelloService.class).hello();
    }
}

 

Spring Boot 2.x
应用属性

spring.main.web-application-type=NONE
# REACTIVE, SERVLET

或SpringApplicationBuilder

@SpringBootApplication
public class MyApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}
其中WebApplicationType:

NONE - The application should not run as a web application and should not start an embedded web server.
REACTIVE - The application should run as a reactive web application and should start an embedded reactive web server.
SERVLET - The application should run as a servlet-based web application and should start an embedded servlet web server.

 

 

完美解决failed to configure a datasource: ‘url‘ attribute is not specified and no em

 https://blog.csdn.net/m0_67390969/article/details/124037486
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2018-04-15 Linux Performance 一文
2018-04-15 linux申请strace ,lstrace, ptrace, dtrace
2018-04-15 linux神器strace
2014-04-15 Church encoding
2014-04-15 jquery 插件JTable使用
2013-04-15 python简介
2012-04-15 C++ 各种排序算法的区别
点击右上角即可分享
微信分享提示