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

网站空间独立ip游戏推广话术技巧

网站空间独立ip,游戏推广话术技巧,做网站什么东西需要费用,新建网站推广给企业1 自动配置原理剖析 1.1 加载配置类的源码追溯 自动配置的触发入口: SpringBootApplication 组合注解是自动配置的起点,其核心包含 EnableAutoConfiguration,该注解使用AutoConfigurationImportSelector 实现配置类的动态加载。 启动类的注…

1 自动配置原理剖析

1.1 加载配置类的源码追溯

  1. 自动配置的触发入口: @SpringBootApplication 组合注解是自动配置的起点,其核心包含 @EnableAutoConfiguration,该注解使用AutoConfigurationImportSelector 实现配置类的动态加载。

ALt

启动类的注解@SpringBootApplication

//可以应用于类、接口(包括注解类型)和枚举声明
@Target(ElementType.TYPE)
//注解信息将在运行时保留
@Retention(RetentionPolicy.RUNTIME)
//在使用 Javadoc 工具生成 API 文档时,该注解将被包含在文档中
@Documented
//如果一个类使用了@SpringBootApplication 注解,那么它的子类也会继承这个注解
@Inherited
//springboot配置类
@SpringBootConfiguration
//核心:启用Spring Boot 的自动配置机制
@EnableAutoConfiguration
//启用组件扫描
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication{}
@SpringBootApplication注解
  1. 查看@EnableAutoConfiguration,@EnableAutoConfiguration注解上导入了AutoConfigurationImportSelector.class,该类负责选择和导入需要自动配置的类
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//指定自动配置的包路径,通常用于扫描带有@Configuration 注解的类
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
3 @EnableAutoConfiguration注解
  1. 查看AutoConfigurationImportSelector.class,该类负责加载、筛选和返回需要加载的自动配置类
public class AutoConfigurationImportSelector implements ... {// 核心方法:选择并加载自动配置类
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {// 1. 检查是否启用自动配置if (!isEnabled(annotationMetadata)) {return NO_IMPORTS; // 如果未启用,返回空数组}// 2. 获取自动配置条目AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);// 3. 返回配置类数组return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
  1. 查看selectImports中调用的getAutoConfigurationEntry方法。
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {// 1. 检查是否启用自动配置if (!isEnabled(annotationMetadata)) {return EMPTY_ENTRY; // 如果未启用,返回空条目}// 2. 获取注解属性AnnotationAttributes attributes = getAttributes(annotationMetadata);// 3. 加载候选配置类List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);// 4. 去重configurations = removeDuplicates(configurations);// 5. 获取排除项Set<String> exclusions = getExclusions(annotationMetadata, attributes);// 6. 检查排除项checkExcludedClasses(configurations, exclusions);// 7. 过滤排除项configurations.removeAll(exclusions);// 8. 应用配置类过滤器configurations = getConfigurationClassFilter().filter(configurations);// 9. 触发事件fireAutoConfigurationImportEvents(configurations, exclusions);// 10. 返回结果return new AutoConfigurationEntry(configurations, exclusions);
}
  1. 查看getAutoConfigurationEntry方法调用的getCandidateConfigurations方法
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {// 1. 加载候选配置类ImportCandidates importCandidates = ImportCandidates.load(this.autoConfigurationAnnotation, getBeanClassLoader());List<String> configurations = importCandidates.getCandidates();// 2. 检查配置类是否为空Assert.notEmpty(configurations,"No auto configuration classes found in " + "META-INF/spring/"+ this.autoConfigurationAnnotation.getName() + ".imports. If you "+ "are using a custom packaging, make sure that file is correct.");// 3. 返回配置类列表return configurations;
}
  1. 查看getCandidateConfigurations调用的load方法,可以看到该方法负责从类路径中加载指定注解对应的 .imports 文件,并解析文件内容。
public static ImportCandidates load(Class<?> annotation, ClassLoader classLoader) {// 1. 检查注解是否为空Assert.notNull(annotation, "'annotation' must not be null");// 2. 决定类加载器ClassLoader classLoaderToUse = decideClassloader(classLoader);// 3. 构建文件路径String location = String.format("META-INF/spring/%s.imports", annotation.getName());// 4. 查找文件 URLEnumeration<URL> urls = findUrlsInClasspath(classLoaderToUse, location);// 5. 读取文件内容List<String> importCandidates = new ArrayList<>();while (urls.hasMoreElements()) {URL url = urls.nextElement();importCandidates.addAll(readCandidateConfigurations(url));}// 6. 返回结果return new ImportCandidates(importCandidates);
}
  1. 最后我们终于知道,@SpringBootApplication通过层层修饰,最后到了load方法,load方法扫描并读取类路径下的.imports文件中的配置类。

1.2 通过条件注解注册配置类

我们选择mybatis的包来介绍如何注册的

  1. 找到mybatis的自动配置包在这里插入图片描述

  2. 查看.imports文件
    在这里插入图片描述

  3. 查看部分配置类:这个类的含义是在满足以下条件时,自动配置并注册一个 FreeMarkerLanguageDriver Bean:
    a、 类路径中存在 FreeMarkerLanguageDriver 和 FreeMarkerLanguageDriverConfig 类。
    b、 Spring 容器中尚未注册 FreeMarkerLanguageDriver 类型的 Bean。

  @Configuration(proxyBeanMethods = false)@ConditionalOnClass({ FreeMarkerLanguageDriver.class, FreeMarkerLanguageDriverConfig.class })public static class FreeMarkerConfiguration {@Bean@ConditionalOnMissingBeanFreeMarkerLanguageDriver freeMarkerLanguageDriver(FreeMarkerLanguageDriverConfig config) {return new FreeMarkerLanguageDriver(config);}
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration配置类的Bean

2 案例:自定义一个自动配置类

  1. 需求:写一个自动配置类,当访问http://localhost:8080/sayhello时,使用控制器使用自动配置类去打印“HELLO!”
  2. 创建业务类
public class GreetingService {private String message = "Hello!";public String sayHello() {return message;}// 支持自定义问候语public void setMessage(String message) {this.message = message;}
}
  1. 创建业务自动配置类
@Configuration
public class GreetingServiceAutoConfiguration {@Beanpublic GreetingService greetingService() {return new GreetingService();}
}
  1. 注册配置
    ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/a6a07b374504405bb2f2afef1d91548a.png

  2. 编写控制器

@RestController
public class HelloController {@Autowired private GreetingService service;@RequestMapping("/hello")public String hello(){return "Hello World";}@RequestMapping("/sayhello")public String sayHello(){return service.sayHello();}
}
  1. 项目结构
    在这里插入图片描述
  2. 效果图
    在这里插入图片描述
http://www.tj-hxxt.cn/news/2687.html

相关文章:

  • 福州网站设计十年乐云seo免费自助建站模板
  • 网站怎么做qq的授权登陆黄页引流推广网站
  • 织梦dede做网站的优点手游推广加盟
  • 浙江信息港证件查询网站搜索优化找哪家
  • 基于互联网 模式下的安全网站建设seo关键词排名优化手机
  • 泰安本地网站津seo快速排名
  • b2c旅游网站建设2021百度最新收录方法
  • 如何做房地产网站seo自然排名关键词来源的优缺点
  • 设计上海地址北京网站建设优化
  • 裤袜 wordpress天津网站seo设计
  • admin5官方地方网站运营全套课程下载营销网站类型
  • html网站的规划与建设6seo是什么意思seo是什么职位
  • 完善校园网站建设草根站长工具
  • 网页编程语言有哪几种潍坊百度快速排名优化
  • 长沙网站制作app开发公司关键词排名点击软件
  • html自学东莞seo建站哪家好
  • 高考评卷工作全面展开网站优化系统
  • 成都房产网最新楼盘北京关键词优化平台
  • 成都网站建设定百度页面推广
  • 京东当前网站做的营销活动百度浏览器电脑版
  • 怎样做网站关键词站长工具忘忧草社区
  • 物联网 网站开发百度官方网站入口
  • 青岛做网站建设价格低商业计划书
  • 中国移动国际精品网seo是什么意思?
  • 台州做网站多少钱谷歌浏览器安卓版下载
  • 有哪些做的好看的网站吗网站的优化和推广方案
  • discuz做网站营销型网站建设的价格
  • 动漫设计软件成都关键词seo推广电话
  • 西宁做网站制作的公司辅导班培训机构
  • 做网站有什么用百一度一下你就知道