SpringBoot三种注解启动方式

1、SpringBoot内置了Tomcat,Jetty等容器,所以可以通过注解方式进行启动。

2、SpringBoot注解启动的原理:SpringBoot main方法,是程序入口,启动SpringBoot项目,创建内置tomcat服务器,使用tomcat加载springmvc 注解启动。注解的作用是让SpringBooot根据应用所声明的依赖来对Spring框架进行自动配置,这个注解告诉SpringBoot根据添加的jar依赖猜想你向如何配置Spring。由于spring-boot-start-web添加了Tomcat和SpringMVC,所以SpringBoot根据注解将假定你正在开发一个web应用并相应的对Spring进行设置

3、第一种注解启动:@EnableAutoConfiguration

@EnableAutoConfiguration 注解的扫包范围默认是当前类,对其他类不启作用。

4、第二种注解启动:@EnableAutoConfiguration+@ComponentScan

@EnableAutoConfiguration+@ComponentScan 可以根据需求通过@ComponentScan(basePackages={"com.demo1.controller","com.demo2.controller"}),指定@EnableAutoConfiguration的扫包范围

5、第三种注解启动:@SpringBootApplication

@SpringBootApplication 等同于@EnableAutoConfiguration+@ComponentScan,但是@SpringBootApplication默认扫同级包及其子包,不用单独再指定扫包范围。

6、建议:SpringBoot的启动类,一般单独抽取出来,并且保证SpringBoot的启动类所在的包,位于所有包的最上方,作为所有子包的父包。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot启动注解是@SpringBootApplication。这个注解是一个组合注解,它包含了三个主要的注解:@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan。其中,@SpringBootConfiguration是一个配置注解,用于标识这个类是一个配置文件类。@EnableAutoConfiguration是用来启用自动配置注解,它会根据类路径下的依赖自动配置Spring应用程序。@ComponentScan是用来扫描和注册Spring的组件的注解。总体来说,@SpringBootApplication注解简化了Spring Boot应用程序的配置启动过程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot启动注解](https://blog.csdn.net/web17508858916/article/details/126511421)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [可能是全网最全的SpringBoot启动流程源码分析(基于 2.1.5 版本)](https://download.csdn.net/download/weixin_38590738/14852963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值