SpringBootApplication注解——史上最全最详细

SpringBootApplication

话不多说,先写三遍:SpringBootApplication、SpringBootApplication、SpringBootApplication

我们先来看看SpringBootApplication里面是啥

1.Target

这是个啥?  它是用于设定注解范围(被描述的注解可以用在什么地方)也急速hi限制了注解的作用域,那啥是注解的范围?注解的范围又有哪些呢?

@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

/** Class, interface (including annotation type), or enum declaration */
    TYPE,
    //类,接口,枚举类
    /** Field declaration (includes enum constants) */
    FIELD,
    //枚举
    /** Method declaration */
    METHOD,
    //只用于方法
    /** Formal parameter declaration */
    PARAMETER,
       
    /** Constructor declaration */
    CONSTRUCTOR,
    //构造方法
    /** Local variable declaration */
    LOCAL_VARIABLE,
    
    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE

@Target(ElementType.TYPE) //注解的作用域
@Retention(RetentionPolicy.RUNTIME) //注解的生存周期
@Documented //可以被文档化
@Inherited //注解用于标注一个父类的注解是否可以被子类继承
@SpringBootConfiguration //声明为一个配置类 proxyBeanMethods是否开启bean代理,默认是true,从IOC容器中取;如果是false则每次获取都是一个新的实例
@EnableAutoConfiguration //开启自动配置---这个将是我们研究的重点
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) //包扫描的规则

 https://www.cnblogs.com/unknows/p/10261539.html

2.Retention

说明:Retention定义了被它注解了的注解可以保留多久,我们点如它的枚举类型看看

    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME

source:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;被编译器忽略

class:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期

runtime:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在

生命周期:runtime>class>source

3.@Documented

注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员。

4.@Inherited

如果一个类用上了@Inherited修饰的注解,那么其子类也会继承这个注解

5.@SpringBootConfiguration

SpringBootConfiguration与Spring中的@Configuation的作用基本一致,只不过@SpringBootConfiguration是springboot的注解,而@Configuration是spring的注解。

  1. @Configuration 可以理解为一个Configuration就是对应的一个Spring的xml版的容器;(beans)

@configuation的作用,实现IOC的关键

 @Configuation等价于<Beans></Beans>

 @Bean等价于<Bean></Bean>

 @ComponentScan等价于<context:component-scan base-package=”com.dxz.demo”/>

6.@EnableAutoConfiguration

自动装配

Target、Retention、Documentd就不用我过多描述了,我们来看看

6.1@AutoConfigurationPackage这个注解

顾名思义:他的中文意思就是自动配置包(我的直译)

我们再来看看@autoConfigurationPackage里面是啥

 6.1.1 @Import

这个注解类是用于导入配置类或者一些需要前置加载的类,你就当作Import 包.类

6.1.2  AutoConfigurationpackages.Registrar.class

这就话其实是引用AutoConfigurationpackages抽象类里面的Registrar类

我们点进去来看看:

 这个类实现了哪些接口我们就不管了,我们直接来看它重写的两个方法:在registerBeanDefinitions方法中,我们可以从左到右读他的方法体,register(register,新建一个PackageImports匿名对象,将metadata这个对象形参填入,然后使用getPackageNames方法填入数组)

其实这就是扫描主配置类同级目录以及子包,然后一一记录下来,并将相应的组件导入到springboot创建管理的容器中。

这里我们大概就明白了@AutoConfigurationPackage这个注解的意思了,其实就是自动扫描包,并记录下来的注解,不过我们要明白的是它的作用范围是同级的包,以及以下的包,如果有包在@AutoConfigurationPackage注解范围以上,那么@AutoConfigurationPackage就扫描不到这个包。

6.2 AutoConfigurationImportSelector.class

我们先看看它的结构

虽然说这个类的变量很多,但是我们仍然可以看到几个带有bean、filter的变量名,那我们可以肯定这个类和bean(类的代理或代言人(实际上确实是通过反射、代理来实现的))以及filter(过滤器)有乱七八糟的关系

猜想已经确定了,我们再来看看方法体,来看我们的猜想是不是正确的。

protected Class<?> getSpringFactoriesLoaderFactoryClass() {
		return EnableAutoConfiguration.class;
	}

这个方法体的名字叫做(我的直译):得到spring的工厂加载工厂类

工厂加载工厂类?我咋感觉像是实现IOC的嘞?我们再来看看返回的是啥?

return EnableAutoConfiguration.class;

返回一个EnableAutoConfiguration类,我知乎内行。

这个方法看不出啥,我们在来看看有没有调用它的方法,搜索了一下还真有。

	/**
	 * Return the auto-configuration class names that should be considered. By default
	 * this method will load candidates using {@link SpringFactoriesLoader} with
	 * {@link #getSpringFactoriesLoaderFactoryClass()}.
	 * @param metadata the source metadata
	 * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
	 * attributes}
	 * @return a list of candidate configurations
	 */
	protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
				getBeanClassLoader());
		Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
				+ "are using a custom packaging, make sure that file is correct.");
		return configurations;
	}

  • 36
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值