2021年7月20日星期二

SpringBoot自动配置原理再理解以及自定义starter

自动配置原理:

先总结下结论:

自动配置的原理大致是Springboot在启动时通过spring.factories中的xxxAutoConfiguration找到xxxProperties类进行默认自动配置(xxxAutoConfiguration类中有很多@Conditional的派生类作用是:当条件不成立时,自动配置会失败),由于xxxProperties类中有@ConfigurationProperties注解会与application.yml配置文件绑定,因此我们可以在application.yml中覆盖掉某些默认配置!

分析自动配置原理:

通过依次点开@SpringBootApplication->@EnableAutoConfiguration->AutoConfigurationImportSelector.class->getAutoConfigurationEntry方法->getCandidateConfigurations方法->loadFactoryNames方法->loadSpringFactories方法

->找到FACTORIES_RESOURCE_LOCATION变量:"META-INF/spring.factories";

我们找到了spring-boot-autoconfigure-2.5.2下的spring.factories文件,如下图所示:

image-20210720155100427

我们以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理;

//表示这是一个配置类,和以前编写的配置文件一样,也可以给容器中添加组件;@Configuration //启动指定类的ConfigurationProperties功能; //进入这个HttpProperties查看,将配置文件中对应的值和HttpProperties绑定起来; //并把HttpProperties加入到ioc容器中@EnableConfigurationProperties({HttpProperties.class}) //Spring底层@Conditional注解 //根据不同的条件判断,如果满足指定的条件,整个配置类里面的配置就会生效; //这里的意思就是判断当前应用是否是web应用,如果是,当前配置类生效@ConditionalOnWebApplication( type = Type.SERVLET)//判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器;@ConditionalOnClass({CharacterEncodingFilter.class})//判断配置文件中是否存在某个配置:spring.http.encoding.enabled; //如果不存在,判断也是成立的 //即使我们配置文件中不配置pring.http.encoding.enabled=true,也是默认生效的;@ConditionalOnProperty( prefix = "spring.http.encoding", value = {"enabled"}, matchIfMissing = true)public class HttpEncodingAutoConfiguration { //他已经和SpringBoot的配置文件映射了 private final Encoding properties; //只有一个有参构造器的情况下,参数的值就会从容器中拿 public HttpEncodingAutoConfiguration(HttpProperties properties) {  this.properties = properties.getEncoding(); }  //给容器中添加一个组件,这个组件的某些值需要从properties中获取 @Bean @ConditionalOnMissingBean //判断容器没有这个组件? public CharacterEncodingFilter characterEncodingFilter() {  CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();  filter.setEncoding(this.properties.getCharset().name());  filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));  filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));  return filter; } //。。。。。。。}

一句话总结 :根据当前不同的条件判断,决定这个配置类是否生效!

  • 一但这个配置类生效;这个配置类就会给容器中添加各种组件;
  • 这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;
  • 所有在配置文件中能配置的属性都是在xxxxProperties类中封装着;
  • 配置文件能配置什么就可以参照某个功能对应的这个属性类
//从配置文件中获取指定的值和bean的属性进行绑定@ConfigurationProperties(prefix = "spring.http") public class HttpProperties { // .....}

我们去配置文件里面试试前缀,看提示!

image-20210720160138156

这就是自动装配的原理!

总结:

1、SpringBoot启动会加载大量的自动配置类

2、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;

3、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件存在在其中,我们就不需要再手动配置了)

4、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;

xxxxAutoConfigurartion:自动配置类;给容器中添加组件

xxxxProperties:封装配置文件中相关属性;

了解:@Conditional

了解完自动装配的原理后,我们来关注一个细节问题,自动配置类必须在一定的条件下才能生效;

@Conditional派生注解(Spring注解版原生的@Conditional作用)

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;

image-20210720160314651

那么多的自动配置类,必须在一定的条件下才能生效;也就是说,我们加载了这么多的配置类,但不是所有的都生效了。

我们怎么知道哪些自动配置类生效?

我们可以通过在application.yaml/properties中启用 debug=true属性;来让控制台打印自动配置报告,这样我们就可以很方便的知道哪些自动配置类生效;

#开启springboot的调试类debug=true

Positive matches:(自动配置类启用的:正匹配)

Negative matches:(没有启动,没有匹配成功的自动配置类:负匹配)

Unconditional classes: (没有条件的类)

自定义starter

我们分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩!

说明:

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

命名归约:

官方命名:

  • 前缀:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web....

自定义命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter

编写启动器

1、在IDEA中新建一个空项目 spring-boot-starter-diy
image-20210720162125328

2、新建一个普通Maven模块:kuang-spring-boot-starter

image-20210720162448363

3、新建一个Springboot模块:kuang-spring-boot-starter-autoconfigure

image-20210720162739787

4、在我们的 starter 中 导入 autoconfigure 的依赖!

image-20210720163150209

5、将 autoconfigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置!

image-20210720164020201

6、首先在autoconfigure 项目中编写一个自己的服务HelloService :

package com.kuang;public class HelloService { HelloProperties helloProperties; public HelloProperties getHelloProperties() {  return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) {  this.helloProperties = helloProperties; } public String sayHello(String name){  return helloProperties.getPrefix() + name + helloProperties.getSuffix(); }}

7、再编写一个HelloProperties 配置类

package com.kuang;import org.springframework.boot.context.properties.ConfigurationProperties;// 前缀 kuang.hello@ConfigurationProperties(prefix = "kuang.hello")public class HelloProperties { private String prefix; private String suffix; public String getPrefix() {  return prefix; } public void setPrefix(String prefix) {  this.prefix = prefix; } public String getSuffix() {  return suffix; } public void setSuffix(String suffix) {  this.suffix = suffix; }}

8、最后编写我们的自动配置类HelloServiceAutoConfiguration:

package com.kuang;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configur......

原文转载:http://www.shaoqun.com/a/889802.html

跨境电商:https://www.ikjzd.com/

epa认证:https://www.ikjzd.com/w/1769

writer:https://www.ikjzd.com/w/1280

pat:https://www.ikjzd.com/w/1079


自动配置原理:先总结下结论:自动配置的原理大致是Springboot在启动时通过spring.factories中的xxxAutoConfiguration找到xxxProperties类进行默认自动配置(xxxAutoConfiguration类中有很多@Conditional的派生类作用是:当条件不成立时,自动配置会失败),由于xxxProperties类中有@ConfigurationPro
笨鸟海淘:https://www.ikjzd.com/w/1550
yeepay:https://www.ikjzd.com/w/2574
芒果店长:https://www.ikjzd.com/w/1533
四月,就该来一趟青岛赏樱花!:http://www.30bags.com/a/268359.html
四月必去的15个地方,我只去过5个,你呢?:http://www.30bags.com/a/248774.html
四月底适合旅游的地方 4月份下旬国内气候宜人好去处:http://www.30bags.com/a/434408.html
四月飞雪杜鹃开,五一西岭迎春来 - :http://www.30bags.com/a/410417.html
口述:我意乱情迷爱上老妈的闺蜜老妈闺蜜儿子:http://www.30bags.com/m/a/249562.html
深圳8月音乐会汇总(持续更新):http://www.30bags.com/a/514994.html
女人是否出轨,以下四个表现都很清楚,男人需要理解:http://lady.shaoqun.com/a/426319.html
亚马逊夏季秒杀窗口开放!USPS于8月上调邮费:https://www.ikjzd.com/articles/146772
2021赣州旅游景点排行榜 江西赣州旅游景点大全 :http://www.30bags.com/a/515002.html

没有评论:

发表评论