spring boot创建非web项目
2023-04-15 20:12 youxin 阅读(120) 评论(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
浙公网安备 33010602011771号