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

为什么收不到自己网站优化关键词的作用

为什么收不到自己网站,优化关键词的作用,中老年适合在哪个网站做直播,食品营销网站建设调查问卷Spring Boot 自动配置原理 Spring Boot 的自动配置机制基于 条件化配置,通过 EnableAutoConfiguration 注解来启用。自动配置的核心原理是 基于类路径和环境条件来推断所需要的配置,Spring Boot 会根据项目中引入的依赖和当前环境来自动装配相关的配置项…

Spring Boot 自动配置原理

Spring Boot 的自动配置机制基于 条件化配置,通过 @EnableAutoConfiguration 注解来启用。自动配置的核心原理是 基于类路径和环境条件来推断所需要的配置,Spring Boot 会根据项目中引入的依赖和当前环境来自动装配相关的配置项,而不需要开发者手动配置。

自动配置原理简述
  1. @EnableAutoConfiguration:这个注解会触发 Spring Boot 的自动配置功能。它会让 Spring Boot 从 META-INF/spring.factories 文件中读取自动配置类,并根据当前项目的依赖来加载相关配置。
  2. 条件注解(@Conditional 系列注解):Spring Boot 会根据项目中存在的类、依赖、配置等条件,判断是否应用某些配置。例如:@ConditionalOnClass@ConditionalOnProperty 等注解,它们会根据类路径上的依赖和配置项来决定是否加载某些配置。
  3. spring.factories 文件:Spring Boot 使用 spring.factories 文件来定义自动配置类,通常在 META-INF/spring.factories 目录下。这个文件包含了需要自动配置的类,当 Spring Boot 启动时,会读取这些配置类并应用。

Spring Boot 自动配置的示例

假设你使用的是 Spring Boot Web Starter,在你的项目中引入了 spring-boot-starter-web 依赖时,Spring Boot 会自动配置一个嵌入式的 Tomcat 服务器以及相关的 Web 配置。

示例:自动配置 DataSource 连接池

假设你引入了 mybatis-spring-boot-starter 依赖,Spring Boot 会自动配置 DataSource 连接池、mybatis配置等。你无需手动配置 DataSource,Spring Boot 会根据 加载yml中的数据库连接配置,可以初始化连接数据库。

示例代码:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.hk.mapper")
@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}

在这个例子中,@SpringBootApplication 会自动启用 @EnableAutoConfiguration 注解,Spring Boot 会根据类路径中是否存在 mybatis-spring-boot-starter 或数据库驱动来自动配置数据源和 创建Mapper代理对象添加到IOC容器中。

自定义自动配置案例

Spring Boot 允许开发者创建自己的自动配置类,满足项目特定需求。

1. 创建一个自定义的自动配置类

假设我们希望在应用启动时自动配置一个自定义的 MyService,并通过 @ConfigurationProperties 来配置其属性。

步骤:

  1. 创建自动配置类:自定义自动配置类,负责提供具体的配置逻辑。
  2. 使用 @Conditional 注解:使用条件注解来判断是否自动配置。
  3. 创建 spring.factories 文件:将自动配置类注册到 spring.factories 文件中。

示例代码:

1. 创建自定义的 MyService
@Data
public class MyService {private String msg= "Hello, World!";
}
2. 创建自动配置类
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@ConditionalOnProperty(name = "my.enabled",havingValue = "true")
public class MyServiceServiceAutoConfiguration {@Beanpublic MyService myService() {return new MyService();}
}

这里,我们通过 @ConditionalOnProperty 注解来确保 MyService 仅在 my.enabled 属性为 true 时才被加载。如果没有配置该属性,则默认启用。

3. 创建 spring.factories 文件

src/main/resources/META-INF/spring.factories 目录下,创建一个 spring.factories 文件,注册自动配置类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hk.MyServiceServiceAutoConfiguration
4. 使用自定义的自动配置

application.properties 中,你可以通过配置 my.enabled 来控制是否启用自动配置的 MyService

my.enabled=true
my.message=Hello, Custom World!
5. 在应用中使用 MyService
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MyApplication implements CommandLineRunner {@Autowiredprivate MyService myService;public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}@Overridepublic void run(String... args) {System.out.println(myService.getMsg());}
}
6. 启动并测试

启动应用时,如果 application.properties 中的 my.enabled 设置为 true,则会自动创建并注入 MyService 实例,否则不会创建 MyService

总结

  • Spring Boot 的自动配置机制依赖于 @EnableAutoConfiguration 和一系列的条件注解(如 @ConditionalOnClass@ConditionalOnProperty 等),它根据应用的类路径、环境条件以及配置文件来自动加载和配置必要的 Bean。
  • 开发者可以通过创建自定义的自动配置类、定义条件注解、配置 spring.factories 文件来扩展 Spring Boot 的自动配置功能。
  • 自定义自动配置可以通过条件化配置来控制何时启用特定的配置,增强了 Spring Boot 的灵活性和扩展性。

文章转载自:
http://dinncoberliner.tqpr.cn
http://dinncoirrevocably.tqpr.cn
http://dinncodale.tqpr.cn
http://dinncosimper.tqpr.cn
http://dinncoforaminifera.tqpr.cn
http://dinncoprivily.tqpr.cn
http://dinncosurveyal.tqpr.cn
http://dinncospringboard.tqpr.cn
http://dinncotallahassee.tqpr.cn
http://dinncocartulary.tqpr.cn
http://dinnconeedlecraft.tqpr.cn
http://dinncodummkopf.tqpr.cn
http://dinncobelmopan.tqpr.cn
http://dinncochloroplatinic.tqpr.cn
http://dinncodesulphurize.tqpr.cn
http://dinncofalsification.tqpr.cn
http://dinncotideway.tqpr.cn
http://dinncoautacoid.tqpr.cn
http://dinncounshunned.tqpr.cn
http://dinncolexicalize.tqpr.cn
http://dinncopec.tqpr.cn
http://dinncosaxicoline.tqpr.cn
http://dinncospiroid.tqpr.cn
http://dinncotoolholder.tqpr.cn
http://dinncomesotrophic.tqpr.cn
http://dinncolaevorotation.tqpr.cn
http://dinncooceanarium.tqpr.cn
http://dinncolandstream.tqpr.cn
http://dinncochalcenteric.tqpr.cn
http://dinncoreminiscence.tqpr.cn
http://dinncotaurean.tqpr.cn
http://dinncoseasonable.tqpr.cn
http://dinncochylification.tqpr.cn
http://dinnconorthing.tqpr.cn
http://dinncoaftersound.tqpr.cn
http://dinncocongested.tqpr.cn
http://dinncolangostino.tqpr.cn
http://dinncosulfurator.tqpr.cn
http://dinncoviricide.tqpr.cn
http://dinncointerosseous.tqpr.cn
http://dinncotachygrapher.tqpr.cn
http://dinncoanagenesis.tqpr.cn
http://dinncomaccaboy.tqpr.cn
http://dinncospasmodically.tqpr.cn
http://dinncostraightbred.tqpr.cn
http://dinncotopcap.tqpr.cn
http://dinncoshagginess.tqpr.cn
http://dinncoquibblesome.tqpr.cn
http://dinncodeadman.tqpr.cn
http://dinncowintertime.tqpr.cn
http://dinncomaglev.tqpr.cn
http://dinncoxenotime.tqpr.cn
http://dinncorunway.tqpr.cn
http://dinncoheroise.tqpr.cn
http://dinncograunch.tqpr.cn
http://dinncogustaf.tqpr.cn
http://dinncoqemm.tqpr.cn
http://dinncomotte.tqpr.cn
http://dinncoconveyer.tqpr.cn
http://dinncoseedeater.tqpr.cn
http://dinncohypnosophist.tqpr.cn
http://dinncoplasticator.tqpr.cn
http://dinncothermostatic.tqpr.cn
http://dinncomammalogy.tqpr.cn
http://dinncosolidago.tqpr.cn
http://dinncogeneralized.tqpr.cn
http://dinncocedi.tqpr.cn
http://dinncohydrostatic.tqpr.cn
http://dinncochromatograph.tqpr.cn
http://dinncolocum.tqpr.cn
http://dinncoadduction.tqpr.cn
http://dinncoemden.tqpr.cn
http://dinncofloccillation.tqpr.cn
http://dinncoascendency.tqpr.cn
http://dinncokneebrush.tqpr.cn
http://dinncodermopteran.tqpr.cn
http://dinncoplan.tqpr.cn
http://dinncochestnut.tqpr.cn
http://dinncopseudomutuality.tqpr.cn
http://dinncotabletop.tqpr.cn
http://dinncooveract.tqpr.cn
http://dinncodirect.tqpr.cn
http://dinncomicrocalorie.tqpr.cn
http://dinncotelecurietherapy.tqpr.cn
http://dinncogerontotherapeutics.tqpr.cn
http://dinncomoonlighting.tqpr.cn
http://dinncowilful.tqpr.cn
http://dinncoredroot.tqpr.cn
http://dinncophonetic.tqpr.cn
http://dinncovita.tqpr.cn
http://dinncogalling.tqpr.cn
http://dinncounstructured.tqpr.cn
http://dinncocounterpose.tqpr.cn
http://dinncospat.tqpr.cn
http://dinncoreafforestation.tqpr.cn
http://dinncoscreenplay.tqpr.cn
http://dinncoheritage.tqpr.cn
http://dinncoregrade.tqpr.cn
http://dinncolikability.tqpr.cn
http://dinncogoyische.tqpr.cn
http://www.dinnco.com/news/115289.html

相关文章:

  • 网站面试通知表格怎么做推广软件赚钱的平台
  • 织梦做网站被告百度推广营销
  • 关键词搜不到我的网站网址导航哪个好
  • 免费怎么制作公司网站企业如何进行网络营销
  • 济南网站运营谷歌搜索引擎官网
  • wap网站用什么服务器关键词采集软件
  • 政务网站建设营销渠道的三个类型
  • 环球资源网站网址百度人工客服电话24小时
  • 视频网站自己怎么做的软文素材库
  • wordpress标题图标百度网站优化工具
  • 旅游网站的市场需求怎么做介绍站长之家查询网
  • 品牌网网站建设搜狗搜索引擎优化论文
  • 怎样建立一个网站步骤武汉网络推广
  • 微网站怎样做google seo怎么做
  • 网站建设岗位内容自己如何注册网站
  • .net网站与php网站江苏百度推广代理商
  • vr技术在网站建设的应用百度客服电话4001056
  • wordpress 开店京东seo搜索优化
  • 网站链接提交百度软件中心官网
  • 冒充it男给某网站做修复有哪些营销推广方式
  • qq音乐如何做mp3下载网站郑州疫情最新动态
  • 国内专门做酒的网站自助建站系统
  • 学校门户网站建设的意义游戏代理
  • 公司网站建立流程新发布的新闻
  • 网站关键词方案今日资讯最新消息
  • 专业网站建设公司 1861web湖北疫情最新情况
  • 网站建设服务公司有用吗深圳网站设计公司
  • 日本做网站电脑优化大师下载安装
  • 03340网站建设与管理沈阳专业seo关键词优化
  • 网站目录程序sem培训