您的位置:首页技术文章
文章详情页

springboot自动装配原理初识

【字号: 日期:2023-03-19 09:55:08浏览:12作者:猪猪
运行原理

为了研究,我们正常从父项目的pom.xml开始进行研究。

pom.xml

父依赖 spring-boot-starter-parent主要用来管理项目的资源过滤和插件

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent>

点父依赖进去查看,发现还有一个父依赖spring-boot-dependencies,这里的这个父依赖才是真正管理springboot应用里面的所有依赖版本的地方,是springboot的版本控制中心。

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.5.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath></parent>

启动器:spring-boot-starter-xxx:springboot的场景启动器

spring-boot-starter-web:导入web依赖的组件

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>主程序

@SpringBootApplication

作用:标注这是一个springboot主程序类,说明这是一个springboot应用,springboot就是运行这个类的mian方法启动的springboot应用。

@SpringBootApplication //标注这是一个主程序类,说明这是一个springboot应用public class Springboot01HelloworldApplication { public static void main(String[] args) { //这里启动了一个服务,而不是执行了一个方法。 SpringApplication.run(Springboot01HelloworldApplication.class, args); }}

点@SpringBootApplication继续研究,会发现有@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan这三个注解

@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})})

1.@ComponentScan: spring自动扫描包

这个我们在spring配置文件中见到过,它用来自动扫描并加载符合条件的组件或者bean,并将bean加载到IOC容器中。

2.@SpringBootConfiguration: springboot的配置类

标注在某个类上,说明这个类是springboot的配置类,在这里它就说明SpringBootApplication这个类是springboot的配置类。

我们继续点@SpringBootConfiguration进去查看,会发现 @Configuration这个注解

2.1 @Configuration:配置类,用来配置spring的xml文件

我们继续点@Configuration进去查看,会发现 @Component这个注解。

2.2 @Component:组件,说明启动类本身也是一个组件,负责启动应用。

至此,@SpringBootConfiguration这条线,我们研究完了。

3.@EnableAutoConfiguration:开启自动装配,通过@EnableAutoConfiguration来帮我们自动配置之前我们需要配置的东西。我们继续点@EnableAutoConfiguration进去查看,会发现 @AutoConfigurationPackage和@Import({AutoConfigurationImportSelector.class}) 这两个注解。

@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.1 @AutoConfigurationPackage自动装配包

继续点进去查看,出现@Import({Registrar.class})这个注解

3.1.1 @Import({Registrar.class}): spring底层注解,给容器导入一个组件

Registrar.class: 将主启动类所在包及所在包下面的所有子包里面所有的组件都扫描到Spring容器。

至此,@AutoConfigurationPackage这条线我们也研究完了。

3.2 @Import({AutoConfigurationImportSelector.class}): 给容器导入组件

AutoConfigurationImportSelector.class:自动装配导入选择器。

导入的选择器分析:

1.我们点进去AutoConfigurationImportSelector.class这个类的源码进行探究,

springboot自动装配原理初识

2.我们点击getCandidateConfigurations进一步分析

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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; }

2.1 使用了getSpringFactoriesLoaderFactoryClass()方法,返回一开始我们看到的启动自动配置文件的注解类EnableAutoConfiguration.class

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

2.2 发现它调用了SpringFactoriesLoader类的静态方法,我们点击loadFactoryNames进入loadFactoryNames()

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) { ClassLoader classLoaderToUse = classLoader; if (classLoader == null) { classLoaderToUse = SpringFactoriesLoader.class.getClassLoader(); } String factoryTypeName = factoryType.getName(); return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList()); }

发现它又调用了loadSpringFactories()方法,点进去查看

private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) { Map<String, List<String>> result = (Map)cache.get(classLoader); if (result != null) { return result; } else { HashMap result = new HashMap(); try {Enumeration urls = classLoader.getResources('META-INF/spring.factories');while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); Iterator var6 = properties.entrySet().iterator(); while(var6.hasNext()) { Entry<?, ?> entry = (Entry)var6.next(); String factoryTypeName = ((String)entry.getKey()).trim(); String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue()); String[] var10 = factoryImplementationNames; int var11 = factoryImplementationNames.length; for(int var12 = 0; var12 < var11; ++var12) { String factoryImplementationName = var10[var12]; ((List)result.computeIfAbsent(factoryTypeName, (key) -> {return new ArrayList(); })).add(factoryImplementationName.trim()); } }}result.replaceAll((factoryType, implementations) -> { return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));});cache.put(classLoader, result);return result; } catch (IOException var14) {throw new IllegalArgumentException('Unable to load factories from location [META-INF/spring.factories]', var14); } } }

源码分析:

MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);获得classLoader,我们返回可以看到这里得到的就是EnableAutoConfiguration标注的类本身 Enumeration<URL> urls = classLoader != null ? classLoader.getResources('META-INF/spring.factories') : ClassLoader.getSystemResources('META-INF/spring.factories');获取一个资源 'META-INF/spring.factories' while循环,读取到的资源遍历,封装成为一个Properties spring.factories文件

springboot自动装配原理初识

WebMvcAutoConfiguration

我们在上面的自动配置类随便找一个打开看看,比如 :WebMvcAutoConfiguration

springboot自动装配原理初识

都是大家熟悉的配置,所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件 ,并将其中对应的 org.springframework.boot.autoconfigure. 包下的配置项,通过反射实例化为对应标注了 @Configuration的JavaConfig形式的IOC容器配置类 , 然后将这些都汇总成为一个实例并加载到IOC容器中。

总结 SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值 将这些值作为自动配置类导入容器,自动配置类就生效,帮我们进行自动配置工作; 整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中; 它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ; 有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作; 主启动类

SpringApplication

@SpringBootApplicationpublic class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); }}

分析:

SpringbootApplication.class:应用参数的入口 args:命令行参数 该方法返回的是一个ConfigurableApplicationContext对象

SpringApplication主要做的事情:

推断应用的类型是普通的项目还是Web项目 查找并加载所有可用初始化器 , 设置到initializers属性中 找出所有的应用程序监听器,设置到listeners属性中 推断并设置main方法的定义类,找到运行的主类

以上就是springboot自动装配原理初识的详细内容,更多关于springboot自动装配原理的资料请关注好吧啦网其它相关文章!

标签: Spring
相关文章: