Spring Boot Application 两种启动方式分析

28 篇文章 1 订阅

Spring Boot Application 两种启动方式分析

问题:

Spring boot一般我们都会有

public static void main(String[] args) {
		SpringApplication.run(xxx.class, args); 
}

但是如果我们使用war包的方式,这个去掉可以吗?

解决过程:

带着这个问题,看了看Spring Boot的文档

Convert an Existing Application to Spring Boot一节中,说到了如何把Spring Mvc转换成Spring Boot的应用。

其中有:

Once the war file is working, you can make it executable by adding a main method to your Application, as shown in the following example:

public static void main(String[] args) {
	SpringApplication.run(Application.class, args);
}

这段话也说明了如果使用war的包,是可以不加main函数的。

总结:

  1. 使用main的方式很直接,就是直接拿SpringApplication进行run。

  2. 使用war方式启动原理如下:

    这里涉及到两个东西:

    • SpringBootServletInitializer 这个类实现了WebApplicationInitializer,代替了传统的web.xml配置,进行配置。在WebApplicationInitializer的注释里说到:

      Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container

      说明只要继承了SpringBootServletInitializer会被当成servlet configuration扫描到并且执行onStartup方法。
      SpringBootServletInitializer进行Application的build和run.

    • @SpringApplication这个注解是一个启动配置的注解。相当于@Configuration, @EnableAutoConfiguration和@ComponentScan。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值