SpringBoot的自动配置原理

不念不忘少年蓝@ 2022-01-28 11:59 409阅读 0赞

SpringBoot的自动配置原理

自动配置原理

配置文件到底能写什么?怎么写?自动配置原理;

配置文件能配置的属性参照 :https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#common-application-properties

1、自动配置原理:

1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能

2)、@EnableAutoConfiguration 作用:

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @AutoConfigurationPackage
  6. @Import({AutoConfigurationImportSelector.class}) //选择器 给容器导入一些组件
  7. public @interface EnableAutoConfiguration {
  8. String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
  9. Class<?>[] exclude() default {};
  10. String[] excludeName() default {};
  11. }

可以查看:

  1. AutoConfigurationImportSelector类中的方法:
  2. @Override
  3. public String[] selectImports(AnnotationMetadata annotationMetadata) {
  4. if (!isEnabled(annotationMetadata)) {
  5. return NO_IMPORTS;
  6. }
  7. AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
  8. .loadMetadata(this.beanClassLoader);
  9. AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
  10. autoConfigurationMetadata, annotationMetadata);
  11. return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
  12. }
  13. protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, //获取到候选的配置
  14. AnnotationAttributes attributes) {
  15. List<String> configurations = SpringFactoriesLoader.loadFactoryNames( //从类路径下得到资源
  16. getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
  17. Assert.notEmpty(configurations,
  18. "No auto configuration classes found in META-INF/spring.factories. If you "
  19. + "are using a custom packaging, make sure that file is correct.");
  20. return configurations;
  21. }

继续点击:

  1. public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
  2. String factoryClassName = factoryClass.getName();
  3. return (List)loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
  4. }

查看方法:

  1. loadSpringFactories
  2. private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
  3. MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
  4. if (result != null) {
  5. return result;
  6. } else {
  7. try { //扫描的文件
  8. Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
  9. LinkedMultiValueMap result = new LinkedMultiValueMap();
  10. while(urls.hasMoreElements()) {
  11. URL url = (URL)urls.nextElement();
  12. UrlResource resource = new UrlResource(url);
  13. Properties properties = PropertiesLoaderUtils.loadProperties(resource); //最后做成了properties
  14. Iterator var6 = properties.entrySet().iterator();
  15. while(var6.hasNext()) {
  16. Entry<?, ?> entry = (Entry)var6.next();
  17. String factoryClassName = ((String)entry.getKey()).trim();
  18. String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
  19. int var10 = var9.length;
  20. for(int var11 = 0; var11 < var10; ++var11) {
  21. String factoryName = var9[var11];
  22. result.add(factoryClassName, factoryName.trim()); //遍历添加到结果集合中
  23. }
  24. }
  25. }
  26. cache.put(classLoader, result);
  27. return result;
  28. } catch (IOException var13) {
  29. throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
  30. }
  31. }
  32. }

总结上面的步骤思路:

利用EnableAutoConfigurationImportSelector给容器中导入一些组件?

可以查看selectImports()方法的内容;
List configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置

  1. SpringFactoriesLoader.loadFactoryNames()
  2. 扫描所有jar包类路径下 METAINF/spring.factories
  3. 把扫描到的这些文件的内容包装成properties对象
  4. properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器

将META-INF/spring.factories EnableAutoConfiguration的值加入到了容器中:

1179709-20190421154839295-1026258980.png

有非常非常多我就不全部截图了,大家有兴趣可以自己看下。每一个这样的xxAutoConfiguration类都是容器总的一个组件,都加入到容器中

用他们做自动配置!

每一个自动配置类,进行自动配置功能。

举一个栗子:

以:org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\ 为例子进行解释自动配置原理

根据当前配置类,决定这个配置类是否生效。

生效了这些组件的属性是从对应的properties类中获取的,这些类是跟配置绑定的

最后通过@Bean给容器添加组件

  1. * {@link EnableAutoConfiguration Auto-configuration} for configuring the encoding to use
  2. * in web applications.
  3. *
  4. * @author Stephane Nicoll
  5. * @author Brian Clozel
  6. * @since 1.2.0
  7. */
  8. @Configuration //表示一个 配置类,以前编写的配置文件一样,也可以给容器中添加组件
  9. @EnableConfigurationProperties(HttpProperties.class) //启用指定类的ConfigurationProperties功能 将配置文件中的值和HttpProperties类进行绑定了 并注册到容器中
  10. @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) //条件 底层spring的@Conditional注解 满足条件就整个配置类生效 当前应用内是否web应用
  11. @ConditionalOnClass(CharacterEncodingFilter.class) //当前jar包(项目)里面有没有这个类 Spring mvc中解决乱码的过滤器 (以前是xml中配置的)
  12. @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", //判断配置文件中是否存在配置 spring.http.encoding
  13. matchIfMissing = true) //即使我们配置文件中 不配置enable属性 也是默认生效的
  14. public class HttpEncodingAutoConfiguration {
  15. private final HttpProperties.Encoding properties;
  16. //根据这个类去 编写properties类 属性进行赋值修改
  17. public HttpEncodingAutoConfiguration(HttpProperties properties) { //只有一个有参构造器的情况下 参数的值会从容器中获取 我们可以通过自定义properties进行修改配置!根据bean来的
  18. this.properties = properties.getEncoding();
  19. }
  20. @Bean
  21. @ConditionalOnMissingBean
  22. public CharacterEncodingFilter characterEncodingFilter() {
  23. CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
  24. filter.setEncoding(this.properties.getCharset().name());
  25. filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST)); //从properties中获取! 这个类 已经和 springboot的配置文件绑定了
  26. filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
  27. return filter;
  28. }
  29. @Bean
  30. public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() {
  31. return new LocaleCharsetMappingsCustomizer(this.properties);
  32. }
  33. private static class LocaleCharsetMappingsCustomizer implements
  34. WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>, Ordered {
  35. private final HttpProperties.Encoding properties;
  36. LocaleCharsetMappingsCustomizer(HttpProperties.Encoding properties) {
  37. this.properties = properties;
  38. }
  39. @Override
  40. public void customize(ConfigurableServletWebServerFactory factory) {
  41. if (this.properties.getMapping() != null) {
  42. factory.setLocaleCharsetMappings(this.properties.getMapping());
  43. }
  44. }
  45. @Override
  46. public int getOrder() {
  47. return 0;
  48. }
  49. }
  50. }

查看类 HttpProperties.class (所有配置文件中能配置的属性都是在xxProperties类中封装着),所以配置文件中能配置什么就可以参照某一个功能对应的这个属性类

  1. @ConfigurationProperties(prefix = "spring.http") //从配置文件中获取值和bean中的属性进行绑定
  2. public class HttpProperties {
  3. /**
  4. * Whether logging of (potentially sensitive) request details at DEBUG and TRACE level
  5. * is allowed.
  6. */
  7. private boolean logRequestDetails;
  8. /**
  9. * HTTP encoding properties.
  10. */
  11. private final Encoding encoding = new Encoding();

所有在配置文件中能配置的属性都是在xxxxProperties类中封装者‘;配置文件能配置什么就可以参照某个功 能对应的这个属性类

精髓:

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

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

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

4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在锁定配置文件,然后指定这 些属性的值;

归纳:

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

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

举个栗子看下:缓存的自动配置 包括了 属性的值获取

1179709-20190421163519431-1024992188.png

点进去看下缓存的配置:

1179709-20190421163629692-517890347.png

补充:

1、@Conditional派生注解(Spring注解版原生的@Conditional作用) 作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;
























































@Conditional扩展注解

  1. 作用(判断是否满足当前指定条件)

@ConditionalOnJava

系统的java版本是否符合要求

@ConditionalOnBean

容器中存在指定Bean;

@ConditionalOnMissingBean

容器中不存在指定Bean;

@ConditionalOnExpression

满足SpEL表达式指定

@ConditionalOnClass

系统中有指定的类

@ConditionalOnMissingClass

系统中没有指定的类

@ConditionalOnSingleCandidate

容器中只有一个指定的Bean,或者这个Bean是首选Bean

@ConditionalOnProperty

  1. 系统中指定的属性是否有指定的值

@ConditionalOnResource

  1. 类路径下是否存在指定资源文件

@ConditionalOnWebApplication

当前是web环境

@ConditionalOnNotWebApplication

当前不是web环境

@ConditionalOnJndi

JNDI存在指定项

自动配置类必须在一定的条件下才能生效!就是 @Conditional搞的

  1. 比如进入AOP自动配置类中,

必须有三个类才能生效,如果当前类没有aspect 的jar包就能生效哦

1179709-20190421165230087-1612845559.png

那么我们怎么看,是否生效啊呢?

在properties中配置, 开启springboot的debug模式

debug=true,这样我们就可以很方便的知道哪些自动配置 类生效;

1179709-20190421172246390-1090306901.png

很多我贴出来了

=========================

AUTO‐CONFIGURATIONREPORT

Positivematches:(自动配置类启用的)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

DispatcherServletAutoConfiguration matched:

‐ @ConditionalOnClass found required class
‘org.springframework.web.servlet.DispatcherServlet’; @ConditionalOnMissingClass did not find
unwanted class (OnClassCondition)
‐ @ConditionalOnWebApplication (required) found StandardServletEnvironment
(OnWebApplicationCondition)
Negativematches:(没有启动,没有匹配成功的自动配置类) ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ActiveMQAutoConfiguration:
Did not match:
‐ @ConditionalOnClass did not find required classes ‘javax.jms.ConnectionFactory’,
‘org.apache.activemq.ActiveMQConnectionFactory’ (OnClassCondition)
AopAutoConfiguration:
Did not match:
‐ @ConditionalOnClass did not find required classes
‘org.aspectj.lang.annotation.Aspect’, ‘org.aspectj.lang.reflect.Advice’ (OnClassCondition)

模式:
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如
果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默
认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

可以一步参考:https://blog.csdn.net/zxc123e/article/details/80222967

posted @ 2019-04-18 10:49 toov5 阅读(…) 评论(…) 编辑 收藏

发表评论

表情:
评论列表 (有 0 条评论,409人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot自动配置原理

    SpringBoot可以简化开发的一个主要原因就是采用了默认配置,所谓约定大于配置就是这个意思。在没有自己指定配置的时候使用默认配置的原理大致如下。如有错误,还请指正。 ==

    相关 SpringBoot自动配置原理

            配置文件到底能写什么?怎么写?自动配置原理; [配置文件能配置的属性参照][Link 1] 1、自动配置原理: 1)、SpringBoot启动的时候加