玩转springboot之springboot主程序



springboot主程序

使用过springboot的都应该知道,springboot的主程序类上是有一个注解@SpringBootApplication的,这个注解表明了这个项目是一个springboot项目,也标注了这个类是springboot的主配置类

这个注解里的内容其实是很多的

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

下面就分别介绍一下这里边的注解

@SpringBootConfiguration

这个注解是用来标注springboot的配置类的,看一下该注解的组成就知道了,是@Configuration注解,spring底层就是来以@Configuration注解标注的类作为配置类的

@Configuration
public @interface SpringBootConfiguration {

}

@EnableAutoConfiguration

这个注解是用来开启自动配置功能的,用来告诉springboot开启自动配置功能,看一下这个注解的构成

看到了@Import注解就知道了,用@Import注解来进行组件导入,还是原来spring的那一套,使用selector选择器来进行操作

@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

这里主要加载了两个文件META-INF/spring-autoconfigure-metadata.properties和META-INF/spring.factories

EnableAutoConfigurationImportSelector的作用是将META-INF/spring.factories文件中写的org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的所有的xxxAutoConfiguration注册进来

接下来看一下@AutoConfigurationPackage里是什么,也是一个@Import注解,使用Registrar注册器来进行组件注册

@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

}

这个AutoConfigurationPackages.Registrar的作用是将被@SpringBootApplication标注的主配置类所在的包以及其子包中的类扫描进spring容器

@ComponentScan

这个注解是用来进行组件扫描的,在spring中就已经存在了,可以自动发现和装配bean

自动配置报告

可以在配置文件中配置来查看哪些自动配置生效了,哪些没生效

debug: true
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches: (生效的自动配置)
-----------------

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)

Negative matches:(没生效的自动配置,以及哪些条件没满足)
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

参考文献

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾光师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值