美文网首页
@SpringBootApplication注解分析

@SpringBootApplication注解分析

作者: 黄海佳 | 来源:发表于2019-03-28 19:56 被阅读0次

    进入@SpringBootApplication,发现他是一个符合注解

    @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}
    )}
    )
    

    其中java中元注解有四个

    @Retention @Target @Document @Inherited

    @Retention:注解的保留位置
    • @Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
    • @Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
    • @Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
    @Target:注解的作用目标
    • @Target(ElementType.TYPE) //接口、类、枚举、注解
    • @Target(ElementType.FIELD) //字段、枚举的常量
    • @Target(ElementType.METHOD) //方法
    • @Target(ElementType.PARAMETER) //方法参数
    • @Target(ElementType.CONSTRUCTOR) //构造函数
    • @Target(ElementType.LOCAL_VARIABLE)//局部变量
    • @Target(ElementType.ANNOTATION_TYPE)//注解
    • @Target(ElementType.PACKAGE) ///包
    @Document:说明该注解将被包含在javadoc中
    @Inherited:说明子类可以继承父类中的该注解

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

    @EnableAutoConfiguration的作用启动自动的配置。
    @EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。在下面博客会具体分析这个注解,快速入门的demo实际没有用到该注解。
    @ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。

    相关文章

      网友评论

          本文标题:@SpringBootApplication注解分析

          本文链接:https://www.haomeiwen.com/subject/botxvqtx.html