创建非Web应用程序

 

目录

官方文档

71.5 Create a non-web application

翻译

71.5 创建非Web应用程序


官方文档

71.5 Create a non-web application

Not all Spring applications have to be web applications (or web services). If you want to execute some code in a main method, but also bootstrap a Spring application to set up the infrastructure to use, then it’s easy with the SpringApplication features of Spring Boot. A SpringApplication changes its ApplicationContext class depending on whether it thinks it needs a web application or not. The first thing you can do to help it is to just leave the servlet API dependencies off the classpath. If you can’t do that (e.g. you are running 2 applications from the same code base) then you can explicitly call setWebEnvironment(false) on your SpringApplication instance, or set the applicationContextClass property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as a CommandLineRunner and dropped into the context as a @Bean definition.

翻译

71.5 创建非Web应用程序

并不是所有的Spring应用都必须是Web应用(或者 WebService)。如果你想在main方法执行一些代码,但是也需要引导Spring应用设置基础框架。然而使用SpringBoot的SpringApplication特性就变得容易多了。一个SpringApplication 改变其上下文ApplicationContext 根据他是否是web应用。第一件事情你可以做的就是把servlet API的依赖从根路径移除掉。如果不能这么做(比如两个应用共用一段基础代码)那么你可以使用 SpringApplication实例设置setWebEnvironment(false) 或者设置applicationContextClass 属性(通过java API或者外部属性文件配置)。希望运行自己业务逻辑的应用程序代码可以实现CommandLineRunner然后通过@Bean放入Context(上下文)中。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值