springboot自动装配原理

springboot帮我们自动配置了什么?我们又能修改什么?

三大核心:

  • spring.factories(各种自动配置类)
  • XXAutoConfiguration(自动配置类)
  • XXProperties(属性注入修改)
1.自动装配原理(最重要)

pom.xml配置导入以来,核心在于父依赖,后面添加的依赖就不需要版本,是因为父依赖spring-boot-dependencies会进行版本控制。

在这里插入图片描述

1.启动器

一个个服务会被变成一个个启动器,这是springboot官方的一个jar包。格式都如: spring-boot-stater-xxx。当然也有其他包整合springboot的,形如:xx-spring-boot-starter

比如springboot-starter-web web服务启动器,还有其他的如jdbc starter

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

2.springboot启动时加载资源:

1.启动的时候这个springbootApplication这个注解 就集成了很多注解,实现了自动装配,加载了该加载的资源。

2.点进这个注解就可以看到一个**@EnableAutoConfiguration** 自动装配的注解,我们再点进这个注解,可以看到上方import了一个自动装配类的选择器AutoConfigurationImportSelector.class。

3.点进这个AutoConfigurationImportSelector类,这个类帮助选择加载哪些自动装配资源,包括相应的自动装配类和静态资源等等。

resourceLoader资源加载器,里面就显示了url是classPath开头的资源都会被加载。

ClassLoader 加载类的。

COnfigurationClassFilter是加载过滤器的。

4.这个AutoConfigurationImportSelector里面有个方法getCandidateConfigurations说明了它加载了/MeTA-INF/spring.factories的自动装配类,并且条件要是EnableAutoConfiguration类的才可以被加载。

  1. 最后加载auto-configure包下的 META-INF/spring.factories**里面的配置

spring.factories存放的所有就是自动配置类:

Properties properties=PropertiesLoaderUtils.loadProperties (resource);
所有的资源都会被封装成roperties供我们配置。

但并不是以上所有自动配置都能自动导入生效,因为还有有个关键注解@ConditionalOnClass ,要根据条件判断是否成立,只要导入了相应start(pom.xml里面的start启动器)对应的启动器才能启动,相应的配置才能自动导入!然后生效!

结构图

这一部分讲解了springboot启动的时候加载了核心的sping.factories文件,里面存放的是各个自动配置类。

3.spring.factories

spring.factories存放的是springboot要加载的核心的类,从这个文件我们就可以看出springboot启动的时候要干些什么。

初始化===》应用监听器===》自动配置监听器===》自动配置==》Failure analyzers错误分析====》Template availability providers模板提供

这里面并不是所有东西都要加载,加载这些东西的时候,有个注解@ConditonXX会对这些自动配置类进行检查,只有满足了 条件之后才会加载,至少pom.xml里面要导入才会启动才会加载。

4.原理再深入

那我们创建一个项目一般至少都要导入spring-boot-starter-web 这个包,那么可以进入spring.facroties会加载WebMvcAutoConfiguration这个自动配置类。

1.WebMvcAutoConfiguration这个类里面可以看到@COnditionalOnClass 它会做一些判定条件。

2.我们看看这个WebAutoConfiguration里面有什么,可以知道它能做些什么?

//部分成员变量
private final WebMvcProperties mvcProperties;//Properties属性相关的配置类
private final ListableBeanFactory beanFactory;//bean相关的
private final ObjectProvider<DispatcherServletPath> dispatcherServletPath;//servlet相关的
private final ObjectProvider<ServletRegistrationBean<?>> servletRegistrations;

从上面我们可以猜测这个WebAutoConfiguration肯定会有一个配置相关的类,这个类应该规定了我们可以通过配置文件配置的属性,bean相关的肯定就是对bean的一个管理,serlvet相关的就是路由controller嘛,包括controller和路由之间的关系怎么寻找,然后用户输入一个url,后台是如何通过url找到对应的controller或者servlet的。

以下是部分的成员方法:

//path匹配
		@Override
		public void configurePathMatch(PathMatchConfigurer configurer) {
			if (this.mvcProperties.getPathmatch()
					.getMatchingStrategy() == WebMvcProperties.MatchingStrategy.PATH_PATTERN_PARSER) {
				configurer.setPatternParser(new PathPatternParser());
			}
			configurer.setUseSuffixPatternMatch(this.mvcProperties.getPathmatch().isUseSuffixPattern());
			configurer.setUseRegisteredSuffixPatternMatch(
					this.mvcProperties.getPathmatch().isUseRegisteredSuffixPattern());
			this.dispatcherServletPath.ifAvailable((dispatcherPath) -> {
				String servletUrlMapping = dispatcherPath.getServletUrlMapping();
				if (servletUrlMapping.equals("/") && singleDispatcherServlet()) {
					UrlPathHelper urlPathHelper = new UrlPathHelper();
					urlPathHelper.setAlwaysUseFullPath(true);
					configurer.setUrlPathHelper(urlPathHelper);
				}
			});
		}


//bean的名字加载
@Bean
@ConditionalOnBean(View.class)
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver() {
   BeanNameViewResolver resolver = new BeanNameViewResolver();
   resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
   return resolver;
}

	//内部的资源视图加载
	@Bean
		@ConditionalOnMissingBean
		public InternalResourceViewResolver defaultViewResolver() {
			InternalResourceViewResolver resolver = new InternalResourceViewResolver();
			resolver.setPrefix(this.mvcProperties.getView().getPrefix());
			resolver.setSuffix(this.mvcProperties.getView().getSuffix());
			return resolver;
		}
//格式化相关的
@Override
		public void addFormatters(FormatterRegistry registry) {
			ApplicationConversionService.addBeans(registry, this.beanFactory);
		}




从这些方法我们可以知道这个类能做一个path路径的匹配,内部资源加载啥的,格式化相关的。

有一个重要的内部类EnableWebMvcConfiguration 这个类实现了 WebMvcConfigurer接口

那是不是意味着我们也可以实现WebMvcConfigurer接口,重写一些方法呢????

然后内部类EnableWebMvcConfiguration 这个就是springboot默认的给我们实现的一个配置,我们可以参考着它来重写WebMvcConfigurer接口。

可以看到如下图,WebMvcConfigureer接口的一些方法,发现它可以重写格式化,添加拦截器addIntercptors(没登陆就不能访问后台管理页面)。

5.XXProperties.

我们在上面进入WebAutoConfiguration类中发现有一个WebMvcProperties,这个properties就决定了我们能在application.properties能配置什么了。

如下图可以看到在application.properties类使用了**@ConfigurationProperties(prefix=‘spring.mvc’)**我们可以使用前缀spring.mvc配置mvc相关的属性了。

不只是WebAutoConfiguration自动配置类有WebMvcProperties,其他自动配置类也有xxProperties,根据xxProperties我们就可以知道application.xml可以配置啥了。。

6.属性赋值

从上方的例子,WebMVCPropertie通过注解**@ConfigurationProperties(prefix=’‘)**实现application.properties能配置的东西,我们也可以自定义一个类,然后通过application.properties进行属性配置。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值