ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot的自动装配原理

2021-07-22 11:35:20  阅读:11  来源: 互联网

标签:装配 return SpringBoot AutoConfigurationPackages 导入 自动 原理 class String


启动类注解

点击查看代码
@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

从SpringBootApplication中可以看到

接下来主要就看

点击查看代码
@EnableAutoConfiguration

这个注解下面又有

点击查看代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage  //自动导入包
@Import(AutoConfigurationImportSelector.class)  //自动导入类中的  自动导入类选择器
public @interface EnableAutoConfiguration {

	/**
	 * Environment property that can be used to override when auto-configuration is
	 * enabled.
	 */
	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

	/**
	 * Exclude specific auto-configuration classes such that they will never be applied.
	 * @return the classes to exclude
	 */
	Class<?>[] exclude() default {};

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * @return the class names to exclude
	 * @since 1.3.0
	 */
	String[] excludeName() default {};

}

自动导入类中AutoConfigurationPackages:

点击查看代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class) //自动导入包中的注册类
public @interface AutoConfigurationPackage {

	/**
	 * Base packages that should be registered with {@link AutoConfigurationPackages}.
	 * <p>
	 * Use {@link #basePackageClasses} for a type-safe alternative to String-based package
	 * names.
	 * @return the back package names
	 * @since 2.3.0
	 */
	String[] basePackages() default {};

	/**
	 * Type-safe alternative to {@link #basePackages} for specifying the packages to be
	 * registered with {@link AutoConfigurationPackages}.
	 * <p>
	 * Consider creating a special no-op marker class or interface in each package that
	 * serves no purpose other than being referenced by this attribute.
	 * @return the base package classes
	 * @since 2.3.0
	 */
	Class<?>[] basePackageClasses() default {};

}

自动导入类选择器:AutoConfigurationImportSelector

从getCandidateConfigurations中

通过进loadFactoryName 进入 

FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

后面它进行了while的循环 是将配置资源遍历最后生成一个porperties文件

结论:springboot启动的时候所有的扫描类都会自动加载  但不一定生效  必须导入stater包  才可以生效

 

标签:装配,return,SpringBoot,AutoConfigurationPackages,导入,自动,原理,class,String
来源: https://www.cnblogs.com/wangjiawei1234/p/15043251.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有