Spring Boot 注解

来源:https://www.cnblogs.com/ldy-blogs/p/8550406.html

Spring Boot 注解

文章目录

  • Spring Boot 注解
    • 1.@SpringBootApplication
    • 2.@ComponentScan
    • 3.@EnableAutoConfiguration
    • 4.@SpringBootConfiguration

1.@SpringBootApplication

@SpringBootAppliction注解包含了@ComponentScan、@Configuration、@EnableAutoConfiguration

它是Spring Boot项目的核心注解,目的是开启自动配置

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication	{}	

2.@ComponentScan

  1. @ComponentScan这个注解在Spring中很重要,它对应XML配置中的元素,@ComponentScan的功能其实就是自动扫描并加载符合条件的组件(比如:@Component和@Repository)或者bean定义,最终将这些Bean定义加载到IOC容器。

    我们可以通过basePackages等属性来细粒度的定制@ComponentScan,自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在的类的basekage进行扫描

  2. @ComponentScan告诉Spring哪个packages的用注解标识的类会被spring自动扫描并且装入bean容器

    例如:@Controller注解标识了,如果不加上@ComponentScan,自动扫描该Controller,那么该Controller就不会被Spring扫描到,更不会装入Spring容器中,因此些@Controller也就没有意义

    basePackageClasses:对basepackages()指定扫描注释组件包类型安全的替代。
     
    excludeFilters:指定不适合组件扫描的类型。
     
    includeFilters:指定哪些类型有资格用于组件扫描。
     
    lazyInit:指定是否应注册扫描的beans为lazy初始化。
     
    nameGenerator:用于在Spring容器中的检测到的组件命名。
     
    resourcePattern:控制可用于组件检测的类文件。
     
    scopedProxy:指出代理是否应该对检测元件产生,在使用过程中会在代理风格时尚的范围是必要的。
     
    scopeResolver:用于解决检测到的组件的范围。
     
    useDefaultFilters:指示是否自动检测类的注释 
    
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
}

3.@EnableAutoConfiguration

@EnableAutoConfiguration简单概括一下就是,借助@Import的支持,收集和注册特定条件的bean定义

  • @EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到IoC容器。
  • @EnableMBeanExport是通过@Import将JMX相关的bean定义加载到IoC容器。

而@EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IOC容器,仅此而已!

@EnableAutoConfiguration作为一个复合Annotation,其自身定义关键信息定义如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    Class<?>[] exclude() default {};
    String[] excludeName() default {};
}

其中,最关键的要属@Import(AutoConfigurationImportSelector.class)借助AutoConfigurationImportSelector,

@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到IOC容器。

Spring Boot 注解_第1张图片

SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件MATE-INF/spring.factories加载配置。

配合@EnableAutoConfiguration使用的话,它更多是提供一种配置查找的功能支持,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的key,获取对应的一组@Configuration类

Spring Boot 注解_第2张图片

上图就是从SpringBoot的autoconfigure依赖包中的META-INF/spring.factories配置文件中摘录的一段内容,可以很好地说明问题。

所以,@EnableAutoConfiguration自动配置:从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration对应的配置项通过反射(Java
Refletion)实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,然后汇总为一个并加载到IoC容器。

4.@SpringBootConfiguration

@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。

你可能感兴趣的:(spring相关注解,spring,boot)