spring boot创建非web项目
2023-04-15 20:12 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/yetaodiao/article/details/126562162
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需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++ 各种排序算法的区别