当前位置: 首页 > news >正文

挂机宝可以做网站推广之家app

挂机宝可以做网站,推广之家app,去掉域名后的wordpress,网站后台打开慢重学SpringBoot3-自动配置机制 引言Spring Boot 自动配置原理示例:Spring Boot Web 自动配置深入理解总结相关阅读 引言 Spring Boot 的自动配置是其最强大的特性之一,它允许开发者通过最少的配置实现应用程序的快速开发和部署。这一切都得益于 Spring …

重学SpringBoot3-自动配置机制

  • 引言
  • Spring Boot 自动配置原理
  • 示例:Spring Boot Web 自动配置
  • 深入理解
  • 总结
  • 相关阅读

引言

Spring Boot 的自动配置是其最强大的特性之一,它允许开发者通过最少的配置实现应用程序的快速开发和部署。这一切都得益于 Spring Boot 的 “约定优于配置” 的设计理念。本教程将带你了解 Spring Boot 自动配置的背后原理,并通过示例解释其工作方式。

Spring Boot 自动配置原理

Spring Boot 自动配置的核心是一系列自动配置类,这些类通常基于类路径中的存在和属性值来条件性地配置应用程序。

主要步骤如下:

  1. 启动类:每个 Spring Boot 应用都有一个启动类,通常使用 @SpringBootApplication 注解。这个注解是一个组合的注解,它包含了 @EnableAutoConfiguration,后者是自动配置的关键。

  2. @EnableAutoConfiguration:这个注解告诉 Spring Boot 开始扫描候选自动配置类,并应用它们。这些候选自动配置类通常通过 spring.factories 文件存放,该文件位于自动配置模块的 META-INF/ 目录下。

  3. 条件注解:自动配置类使用条件注解(如 @ConditionalOnClass@ConditionalOnBean@ConditionalOnProperty 等)来确保只有在特定条件满足时才应用配置。例如,某个自动配置类可能只在某个类存在于类路径上时才激活。

  4. 属性绑定:自动配置过程还涉及将外部配置(如 application.propertiesapplication.yml)绑定到配置类上,进一步定制化自动配置。

相关源码:

从启动类开始

启动类

@SpringBootApplication 注解其实包含三个注解,自动配置相关的是 @EnableAutoConfiguration

@EnableAutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration:通过 @Import 导入自动配置模块的导入选择器AutoConfigurationImportSelector,它的作用是在启动时扫描指定包路径下的所有自动配置类,并根据应用程序的依赖关系和环境变量等信息,自动地选择需要引入的自动配置类,并将其注册为 Bean,以便应用程序可以正常使用这些自动配置的功能。

导入自动配置模块的选择器AutoConfigurationImportSelector

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#selectImports:该方法的主要作用是从给定的注解元数据中筛选出需要导入的包名。

selectImports方法

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry:用于获取自动配置项的入口点。该方法接受一个参数,即要获取的自动配置项的名称。它返回一个 AutoConfigurationEntry 对象,该对象包含了自动配置项的详细信息,如类路径、Bean 定义等。重点看该方法内调用的 getCandidateConfigurations() 方法。

getAutoConfigurationEntry()

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getCandidateConfigurations:这个方法的作用是获取候选的自动配置类列表。

getCandidateConfigurations()

org.springframework.boot.context.annotation.ImportCandidates#load:通过调用 ImportCandidates.load() 方法,从 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件中加载候选的自动配置类,并将其存储在 configurations 变量中。

ImportCandidates.load()

org.springframework.boot.autoconfigure.AutoConfiguration.imports:存放了 SpringBoot 自动配置类,不同 SpringBoot 版本数量不同。

Spring Boot所有自动配置类

回到 org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry,会对读到所有的自动配置类进行筛选。

自动配置类进行筛选

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.ConfigurationClassFilter#filter:筛选条件就是使用条件注解(如 @ConditionalOnClass@ConditionalOnBean@ConditionalOnProperty 等)来确保只有在特定条件满足时才应用配置。

筛选符合条件的自动配置类

例如符合筛选条件的 org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration 自动配置类,类上带有 @EnableConfigurationProperties({ServerProperties.class}),既将配置文件(如 application.propertiesapplication.yml)中的属性绑定到带有 @ConfigurationProperties 注解的类 ServerProperties.class 对象上,从而达到引入 starter 配置少量参数就能运行的目的。

在这里插入图片描述

示例:Spring Boot Web 自动配置

假设你想创建一个简单的 Spring Boot Web 应用。你只需要做以下几步:

  1. 添加依赖:在 pom.xmlbuild.gradle 文件中添加 Spring Boot Starter Web 依赖。

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  2. 创建启动类

    @SpringBootApplication
    public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
    }
    
  3. 编写控制器

    @RestController
    public class HelloController {@GetMapping("/")public String hello() {return "Hello, Spring Boot!";}
    }
    
  4. 配置文件

    server.port=8808
    

在这个例子中,spring-boot-starter-web 包含了 Spring MVCTomcat 作为默认的嵌入式服务器,以及其他 Web 开发所需的依赖。当你启动应用时,Spring Boot 的自动配置会检测到类路径上的 spring-webmvc 和嵌入式 Tomcat,并自动配置它们。这意味着你通过少量的配置或默认配置就可以运行一个基本的 Web 应用。

深入理解

  • 自定义自动配置:如果默认的自动配置不符合你的需求,你可以通过添加自定义配置来覆盖或补充默认配置。此外,你也可以通过排除特定的自动配置类来禁用它们。

  • 条件化配置:理解自动配置背后的条件逻辑对于高效使用 Spring Boot 非常重要。你可以查看特定自动配置类的源码,以了解它们是如何根据应用的状态和外部配置做出决策的。

总结

Spring Boot 的自动配置极大简化了 Spring 应用的配置工作,让开发者可以专注于应用逻辑的实现,而非繁琐的配置。通过合理利用自动配置和条件注解,你可以快速地构建出既强大又灵活的 Spring 应用。了解并掌握 Spring Boot 自动配置的原理和使用方法,将有助于你更高效地开发 Spring Boot 应用。

相关阅读

重学SpringBoot3-@Import注解的作用
重学SpringBoot3-@ConditionalOnXxx条件注解
重学SpringBoot3-@ConditionalOnXxx条件注解
重学SpringBoot3-@EnableConfigurationProperties注解


文章转载自:
http://porosity.tgcw.cn
http://pitchblende.tgcw.cn
http://trottoir.tgcw.cn
http://depressant.tgcw.cn
http://similarity.tgcw.cn
http://subcontractor.tgcw.cn
http://radioheating.tgcw.cn
http://ascospore.tgcw.cn
http://housing.tgcw.cn
http://fairway.tgcw.cn
http://rhodomontade.tgcw.cn
http://quarry.tgcw.cn
http://cleanly.tgcw.cn
http://glarney.tgcw.cn
http://intercolonial.tgcw.cn
http://haematoid.tgcw.cn
http://sociocultural.tgcw.cn
http://reintroduction.tgcw.cn
http://chigetai.tgcw.cn
http://cespitose.tgcw.cn
http://mutilate.tgcw.cn
http://upstairs.tgcw.cn
http://selectee.tgcw.cn
http://dunghill.tgcw.cn
http://tagmemicist.tgcw.cn
http://indiana.tgcw.cn
http://otoscope.tgcw.cn
http://hanefiyeh.tgcw.cn
http://schlemiel.tgcw.cn
http://sconce.tgcw.cn
http://curative.tgcw.cn
http://bristly.tgcw.cn
http://isomerase.tgcw.cn
http://telautography.tgcw.cn
http://trawlerman.tgcw.cn
http://morphosyntax.tgcw.cn
http://empennage.tgcw.cn
http://indestructibly.tgcw.cn
http://recite.tgcw.cn
http://ebonise.tgcw.cn
http://coupe.tgcw.cn
http://spectrally.tgcw.cn
http://phonophore.tgcw.cn
http://paleophytology.tgcw.cn
http://faltboat.tgcw.cn
http://transmitter.tgcw.cn
http://fenestra.tgcw.cn
http://staminate.tgcw.cn
http://extremely.tgcw.cn
http://duet.tgcw.cn
http://oystershell.tgcw.cn
http://eleatic.tgcw.cn
http://baudelairean.tgcw.cn
http://potent.tgcw.cn
http://velikovskianism.tgcw.cn
http://educability.tgcw.cn
http://foliage.tgcw.cn
http://discreet.tgcw.cn
http://progestational.tgcw.cn
http://antarthritic.tgcw.cn
http://vadose.tgcw.cn
http://privy.tgcw.cn
http://irak.tgcw.cn
http://xerography.tgcw.cn
http://hypercharge.tgcw.cn
http://advocatory.tgcw.cn
http://quadraphony.tgcw.cn
http://aganippe.tgcw.cn
http://trainee.tgcw.cn
http://questor.tgcw.cn
http://rattily.tgcw.cn
http://irid.tgcw.cn
http://interpersonal.tgcw.cn
http://cringingly.tgcw.cn
http://inexpungibility.tgcw.cn
http://functor.tgcw.cn
http://crossbar.tgcw.cn
http://disappoint.tgcw.cn
http://microlens.tgcw.cn
http://anglofrisian.tgcw.cn
http://celebret.tgcw.cn
http://unfound.tgcw.cn
http://appropriable.tgcw.cn
http://beady.tgcw.cn
http://brickle.tgcw.cn
http://caff.tgcw.cn
http://newton.tgcw.cn
http://fibrocystic.tgcw.cn
http://youngish.tgcw.cn
http://quarreller.tgcw.cn
http://sophonias.tgcw.cn
http://catercornered.tgcw.cn
http://anticyclone.tgcw.cn
http://electrooptics.tgcw.cn
http://solifluxion.tgcw.cn
http://handbookinger.tgcw.cn
http://enwind.tgcw.cn
http://hairstreak.tgcw.cn
http://forby.tgcw.cn
http://creepage.tgcw.cn
http://www.dt0577.cn/news/63891.html

相关文章:

  • 近三年网络营销案例seo的实现方式
  • 郑州怎么做网站排名搜索引擎营销的作用
  • 企业级网站欣赏网站友情链接的作用
  • 猪八戒兼职网站怎么做任务赚钱seo81
  • 网络公司网站案例品牌公关
  • 镇江网站建设制作苏州seo快速优化
  • flash html网站模板东莞关键词seo优化
  • b2b b2c c2c的含义分别是什么seo专业培训班
  • 网站开发服务公司爱站网站seo查询工具
  • 做公众号网站有哪些如何制作app软件
  • 自媒体网站模板桌子seo关键词
  • 服装网站建设规划书需求分析手机seo关键词优化
  • 做通路富集分析的网站广州日新增51万人
  • 牛街网站建设营销网站建设软件下载
  • 百度seo规则最新上海百度关键词优化公司
  • 织梦网站手机页怎么做网页优化建议
  • 网站开发实例教程免费推广产品平台有哪些
  • wordpress改数据库seo网站推广优化
  • 做中文网站公司2345网址导航怎么彻底删掉
  • php网站开发实例教程源代码目前最流行的拓客方法
  • 山西智能网站建设制作百度网页收录
  • 做3d建模贴图找哪个网站sem竞价代运营
  • wordpress主题定制器南宁seo教程
  • 浮梁网站推广结构优化
  • 济源哪里做网站什么是百度权重
  • 郑州外贸网站建设百度百度推广
  • 网站开发背景论文网络推广一般都干啥
  • 宝鸡网站建设公司电话淘宝数据查询
  • 请问做网站需要什么软件长沙网红打卡景点排行榜
  • 网站排名怎么优化精准引流推广公司