@SpringBootApplication 注解

@SpringBootApplication注解实际上是Spring Boot提供的一个复合注解

其中三个比较重要的注解@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

1@SpringBootConfiguration

点@SpringBootConfiguration注解不难看出@SpringBootConfiguration 也是来源于 @Configuration,二者功能都是将当前类标注为配置类,并将当前类里以 @Bean 注解标记的方法的实例注入到Spring容器中,实例名即为方法名。 

@EnableAutoConfiguration

@EnableAutoConfiguration 注解启用自动配置,其可以帮助 Spring Boot 应用将所有符合条件的 @Configuration 配置都加载到当前 IoC 容器之中。

具体分析:

  • @EnableAutoConfiguration 借助 AutoConfigurationImportSelector 的帮助,而后者通过实现 selectImports() 方法来导出 Configuration

  • AutoConfigurationImportSelector类的 selectImports() 方法里面通过调用Spring Core 包里 SpringFactoriesLoader类的loadFactoryNames()方法

  • 最终通过 SpringFactoriesLoader.loadFactoryNames(),在内部的函数调用里读取了 ClassPath 下面的 META-INF/spring.factories 文件来获取所有导出类。

@ComponentScan

@ComponentScan 对应于XML配置形式中的 <context:component-scan>,用于将一些标注了特定注解的bean定义批量采集注册到Spring的IoC容器之中,这些特定的注解大致包括

@Controller、@Entity、@Component、@Service、@Repository等

对于该注解,还可以通过 basePackages 属性来更细粒度的控制该注解的自动扫描范围

如basePackages = {"com.liwei.core"}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值