spring boot实战自动配置原理分析_spring boot自动配置原理、实战(1)

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

该注解上存在元注解@EnableAutoConfiguration,这就是Spring Boot自动配置实现的核心入口;其定义为:

[html] 
view plain
 copy

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @Import({ EnableAutoConfigurationImportSelector.class,
  6. AutoConfigurationPackages.Registrar.class })
  7. public @interface EnableAutoConfiguration {
  8. /**
  9. * Exclude specific auto-configuration classes such that they will never be applied.
  10. * @return the classes to exclude
  11. */
  12. Class<?>[] exclude() default {};
  13. }

很显然能看出有一特殊的注

解@Import,该注解在spring boot实战(第十篇)Spring boot Bean加载源码分析中有讲解到,加载bean时会解析Import注解,因此需要讲目光聚集在这段代码

[html] 
view plain
 copy

  1. @Import({ EnableAutoConfigurationImportSelector.class,
  2. AutoConfigurationPackages.Registrar.class })

EnableAutoConfigurationImportSelector

来看EnableAutoConfigurationImportSelector类

[html] 
view plain
 copy

  1. public String[] selectImports(AnnotationMetadata metadata) {
  2. try {
  3. AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
  4. .getAnnotationAttributes(EnableAutoConfiguration.class.getName(),
  5. true));
  6. Assert.notNull(attributes, "No auto-configuration attributes found. Is "
  7. + metadata.getClassName()
  8. + " annotated with @EnableAutoConfiguration?");
  9. // Find all possible auto configuration classes, filtering duplicates
  10. List factories = new ArrayList(new LinkedHashSet(
  11. SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
  12. this.beanClassLoader)));
  13. // Remove those specifically disabled
  14. factories.removeAll(Arrays.asList(attributes.getStringArray(“exclude”)));
  15. // Sort
  16. factories = new AutoConfigurationSorter(this.resourceLoader)
  17. .getInPriorityOrder(factories);
  18. return factories.toArray(new String[factories.size()]);
  19. }
  20. catch (IOException ex) {
  21. throw new IllegalStateException(ex);
  22. }
  23. }

看如下代码,获取类路径下spring.factories下key为EnableAutoConfiguration全限定名对应值

[html] 
view plain
 copy

  1. SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
  2. this.beanClassLoader))

其结果为:

[html] 
view plain
 copy

  1. # Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  4. org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
  5. org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
  6. org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
  7. org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
  8. org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
  9. org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
  10. org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
  11. org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
  12. org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
  13. org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
  14. org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
  15. org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
  16. org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
  17. org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
  18. org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
  19. org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
  20. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
  21. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
  22. org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
  23. org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
  24. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
  25. org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
  26. org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
  27. org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
  28. org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
  29. org.springframework.boot.autoconfigure.jms.hornetq.HornetQAutoConfiguration,\
  30. org.springframework.boot.autoconfigure.jta.JtaAutoConfiguration,\
  31. org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration,\
  32. org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration,\
  33. org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
  34. org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
  35. org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
  36. org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
  37. org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
  38. org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
  39. org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
  40. org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
  41. org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
  42. org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\
  43. org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
  44. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
  45. org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
  46. org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
  47. org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
  48. org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
  49. org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
  50. org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
  51. org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
  52. org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
  53. org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
  54. org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration,\
  55. org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
  56. org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
  57. org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
  58. org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
  59. org.springframework.boot.autoconfigure.web.GzipFilterAutoConfiguration,\
  60. org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
  61. org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
  62. org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
  63. org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
  64. org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
  65. org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration

以上为Spring Boot中所有的自动配置相关类;在启动过程中会解析对应类配置信息,以RabbitMQ为例,则会去解析RabbitAutoConfiguration

RabbitAutoConfiguration

首先来看RabbitAutoConfiguration类上的注解:

[html] 
view plain
 copy

  1. @Configuration
  2. @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
  3. @EnableConfigurationProperties(RabbitProperties.class)
  4. @Import(RabbitAnnotationDrivenConfiguration.class)
  5. public class RabbitAutoConfiguration {
  • @Configuration: 应该不需要解释
  • @ConditionalOnClass:表示存在对应的Class文件时才会去解析RabbitAutoConfiguration,否则直接跳过不解析,这也是为什么在不导入RabbitMQ依赖Jar时工程能正常启动的原因
  • @EnableConfigurationProperties:表示对@ConfigurationProperties的内嵌支持,默认会将对应Class这是为bean,例如这里值为RabbitProperties.class,其定义为:

[html] 
view plain
 copy

  1. @ConfigurationProperties(prefix = “spring.rabbitmq”)
  2. public class RabbitProperties {
  3. /**
  4. * RabbitMQ host.
  5. */
  6. private String host = “localhost”;
  7. /**
  8. * RabbitMQ port.
  9. */
  10. private int port = 5672;   … //省略部分代码}

RabbitProperties提供对RabbitMQ的配置信息,其前缀为spring.rabbitmq,因此在上篇中配置的host、port等信息会配置到该类上,随后@
EnableConfigurationProperties会将RabbitProperties注册为一个bean。

  • @Import为导入配置,RabbitAnnotationDrivenConfiguration具体实现如下:

[html] 
view plain
 copy

  1. @Configuration
  2. @ConditionalOnClass(EnableRabbit.class)

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

@ConditionalOnClass(EnableRabbit.class)

[外链图片转存中…(img-FDehV0Gs-1715683968755)]
[外链图片转存中…(img-71KP5BNO-1715683968755)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

  • 11
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值