zoukankan      html  css  js  c++  java
  • @SpringBootApplication了解学习

    @SpringBootApplication

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    package org.springframework.boot.autoconfigure;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.boot.context.TypeExcludeFilter;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.core.annotation.AliasFor;
    
    @Target({ElementType.TYPE})//定义在一个注解类的前面,用来说明该注解可以被声明在哪些元素前。(默认可以放在任何元素之前)
    @Retention(RetentionPolicy.RUNTIME)//定义在一个注解类的前面,用来说明该注解的生命周期
    @Documented//表明在生成JavaDoc文档时,该注解也会出现在javaDoc文档中
    @Inherited//明该注解将会被子类继承
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    public @interface SpringBootApplication {
        @AliasFor(
            annotation = EnableAutoConfiguration.class
        )
        Class<?>[] exclude() default {};
    
        @AliasFor(
            annotation = EnableAutoConfiguration.class
        )
        String[] excludeName() default {};
    
        @AliasFor(
            annotation = ComponentScan.class,
            attribute = "basePackages"
        )
        String[] scanBasePackages() default {};
    
        @AliasFor(
            annotation = ComponentScan.class,
            attribute = "basePackageClasses"
        )
        Class<?>[] scanBasePackageClasses() default {};
    }
    
    

    **这个注解除了四个元注解(注解的注解)其实最核心的就以下三个: **

    1.@SpringBootConfiguration:看它的源码,说白了就是@Configuration, 标注当前类是配置类 ,用于定义配置类。

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration
    public @interface SpringBootConfiguration {
    }
    

    2.@EnableAutoConfiguration: 能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。

    springboot约定大于配置:自动配置在加载一个类的时候,会首先去读取项目当中的配置文件,假如没有,就会启用默认值,这就是springboot约定大于配置原理。

    @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 {};
    }
    
    

    3.@ComponentScan: 会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    @Documented
    @Repeatable(ComponentScans.class)
    public @interface ComponentScan {
        @AliasFor("basePackages")
        String[] value() default {};
    
        @AliasFor("value")
        String[] basePackages() default {};
    
        Class<?>[] basePackageClasses() default {};
    
        Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
    
        Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
    
        ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
    
        String resourcePattern() default "**/*.class";
    
        boolean useDefaultFilters() default true;
    
        ComponentScan.Filter[] includeFilters() default {};
    
        ComponentScan.Filter[] excludeFilters() default {};
    
        boolean lazyInit() default false;
    
        @Retention(RetentionPolicy.RUNTIME)
        @Target({})
        public @interface Filter {
            FilterType type() default FilterType.ANNOTATION;
    
            @AliasFor("classes")
            Class<?>[] value() default {};
    
            @AliasFor("value")
            Class<?>[] classes() default {};
    
            String[] pattern() default {};
        }
    }
    

    关于元注解的个人看法

    在大多数情况中,我觉得在开发中使用元注解的情况不多。因为元注解意思就是“对注解的注解”,除非你想自己写一个注解,不然基本不会用到元注解。但对它有个基本的了解却是必须的。

    以下就是六个元注解

    @Target:定义在一个注解类的前面,用来说明该注解可以被声明在哪些元素前。(默认可以放在任何元素之前)

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         *返回注释类型的元素类型数组
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }
    

    @Retention:定义在一个注解类的前面,用来说明该注解的生命周期

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * 返回保留策略。
         */
        RetentionPolicy value();
    }
    

    @Documented: 表示javadoc或者其他简单工具会将该注解类型文档化

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Documented {
    }
    

    @Inherited://明该注解将会被子类继承

    以上四个注解都是在从1.5后就开始使用

    下面这两个注解1.8开始使用

    @Repeatable: 这个声明这个注解修饰的类型注解是可以重复的。

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Repeatable {
        /**
         * Indicates the <em>containing annotation type</em> for the
         * repeatable annotation type.
         * @return the containing annotation type
         */
        Class<? extends Annotation> value();
    }
    
    
    

    @Native

    @Documented
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Native {
    }
    /**
    *指示可以引用定义常量值的字段
    
    *来自本机代码。
    
    *生成本机的工具可以使用注释作为提示
    
    *头文件以确定是否需要头文件,以及
    
    *如果是,它应该包含哪些声明。
    */
    
  • 相关阅读:
    Axis,axis2,Xfire以及cxf对比 (转)
    客户端调用服务器端三种方式
    webservice发送数据,取数据的方式
    强制关闭myeclipse出现的问题
    取当前系统的上月时间
    oracle 报Ora-01008错误:oracle 并非所有变量都已绑定的原因.TO_number();动态执行select..into..语句时
    ResultSetMetaData rsmd = rs.getMetaData()是什么意思?
    概率质量函数:怀孕周期的PMF
    Python 正则表达式
    TF-IDF
  • 原文地址:https://www.cnblogs.com/lzxulxy/p/11670557.html
Copyright © 2011-2022 走看看