2024年3月6日
说明 源码阅读仓库: spring-cloud-commons 参考资料和需要掌握的知识: SpringBoot 源码分析 Spring 源码分析 Spring Cloud 官网文档 Spring Cloud Commons 官网文档 Spring Cloud 介绍 SpringCloud 是在 SpringBoot 的基础上构建的。Spring Cloud 以两个库的形式提供了许多特性:Spring Cloud Context 和 Spring Cloud Commons。Spring Cloud Context 为 SpringCloud 应用程序的 ApplicationContext 提供扩展机制(引导上下文、加密、刷新属性和……
阅读全文
2024年3月6日
SpringBootBatch 源码 加载 版本使用 2.7.13 1 2 3 4 5 6 7 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.13</version> <scope>import</scope> <type>pom</type> </dependency> spring-autoconfigure-metadata.properties 1 2 3 4 5 6 7 8 9 10 11 12 13 org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration= org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration$DataSourceInitializerConfiguration= org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration$DataSourceInitializerConfiguration.ConditionalOnBean=javax.sql.DataSource org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration$DataSourceInitializerConfiguration.ConditionalOnClass=org.springframework.jdbc.datasource.init.DatabasePopulator org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration.ConditionalOnBean=org.springframework.batch.core.launch.JobLauncher org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration.ConditionalOnClass=javax.sql.DataSource,org.springframework.batch.core.launch.JobLauncher org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration= org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JpaBatchConfiguration= org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JpaBatchConfiguration.ConditionalOnBean= org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JpaBatchConfiguration.ConditionalOnClass=javax.persistence.EntityManagerFactory org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration.ConditionalOnBean=javax.sql.DataSource org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration.ConditionalOnClass=org.springframework.transaction.PlatformTransactionManager @EnableBatchProcessing 启动首先添加@EnableBatchProcessing,这个类引入了BatchConfigurationSelector. BatchConfigurationSelector 这里面主要是判断modular决定加载Modula……
阅读全文
2024年3月6日
Spring Boot 自动装配 Author: HuiFer 源码阅读仓库: SourceHot-spring-boot org.springframework.boot.autoconfigure.SpringBootApplication 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { @AliasFor(annotation = EnableAutoConfiguration.class) Class<?>[] exclude() default {}; @AliasFor(annotation = EnableAutoConfiguration.class) String[] excludeName() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") Class<?>[] scanBasePackageClasses() default {}; @AliasFor(annotation = Configuration.class) boolean proxyBeanMethods() default true; } EnableAutoConfiguration 1 2 3 4 5 6 7 8 9 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { } AutoConfigurationImportSelector 类图 getAutoConfigurationMetadata() 1 2 3 4……
阅读全文
2024年3月6日
Spring Boot application 文件加载 Author: HuiFer 源码阅读仓库: SourceHot-spring-boot 如何找到这个加载的过程 创建配置文件application.yml 全局搜索 yml 换成properties搜索 我们以yml为例打上断点开始源码追踪 看到调用堆栈 一步一步回上去看如何调用具体方法的 ConfigFileApplicationListener 配置文件监听器 调用过程 org.springframework.boot.context.config.ConfigFileApplicationListener#addPropertySources 1 2 3 4 5 protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { RandomValuePropertySource.addToEnvironment(environment); // 加载器加……
阅读全文
2024年3月6日
SpringBoot 日志系统 Author: HuiFer 源码阅读仓库: SourceHot-spring-boot 包路径: org.springframework.boot.logging 日志级别 日志级别: org.springframework.boot.logging.LogLevel 1 2 3 public enum LogLevel { TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF } Java 日志实现 org.springframework.boot.logging.java.JavaLoggingSystem 1 2 3 4 5 6 7 8 9 10 static { // KEY : springBoot 定义的日志级别, value: jdk 定义的日志级别 LEVELS.map(LogLevel.TRACE, Level.FINEST); LEVELS.map(LogLevel.DEBUG, Level.FINE); LEVELS.map(LogLevel.INFO, Level.INFO); LEVELS.map(LogLevel.WARN, Level.WARNING); LEVELS.map(LogLevel.ERROR, Level.SEVERE); LEVELS.map(LogLevel.FATAL, Level.SEVERE); LEVELS.map(LogLevel.OFF, Level.OFF); } LEVELS 对象 1 2 3 4 5 6 7 8 9 10 protected static class LogLevels<T> { /** * key : SpringBoot 中定义的日志级别, value: 其他日志框架的日……
阅读全文
2024年3月6日
SpringBoot ConfigurationProperties Author: HuiFer 源码阅读仓库: SourceHot-spring-boot 本文主要对org.springframework.boot.context.properties.ConfigurationProperties进行分析 ConfigurationProperties 顶部注释 1 2 3 4 * @see ConfigurationPropertiesScan * @see ConstructorBinding * @see ConfigurationPropertiesBindingPostProcessor * @see EnableConfigurationProperties 看到ConfigurationPropertiesScan 去看……
阅读全文
2024年3月6日
SpringBoot ConditionalOnBean Author: HuiFer 源码阅读仓库: SourceHot-spring-boot 在 SpringBoot 中有下列当 XXX 存在或不存的时候执行初始化 ConditionalOnBean ConditionalOnClass ConditionalOnCloudPlatform ConditionalOnExpression ConditionalOnJava ConditionalOnJndi ConditionalOnMissingBean ConditionalOnMissingClass ConditionalOnNotWebApplication ConditionalOnProperty ConditionalOnResource ConditionalOnSingleCandidate ConditionalOnWebApplication ConditionalOnBean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnBeanCondition.class) public @interface ConditionalOnBean { /** * 需要匹配的 bean 类型 */ Class<?>[] value() default {}; /** * 需要匹配的 bean 类型 */ String[] type() default {}; /** * 匹配的 bean 注解 */ Class<? extends Annotation>[] annotation() default {}; /**……
阅读全文
2024年3月6日
SpringBoot 启动方法 Author: HuiFer 源码阅读仓库: SourceHot-spring-boot 入口 通常一个简单的 SpringBoot 基础项目我们会有如下代码 1 2 3 4 5 6 7 8 9 10 @SpringBootApplication @RestController @RequestMapping("/") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 值得关注的有SpringApplication.run以及注解@SpringBootApplication run 方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20……
阅读全文
2024年3月6日
MappingRegistry Author: HuiFer 源码阅读仓库: SourceHot-spring 源码路径: org.springframework.jms.annotation.EnableJms 类全路径 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry 基本属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 class MappingRegistry { /** * key:mapping * value: mapping registration */ private final Map<T, MappingRegistration<T>> registry = new HashMap<>(); /** * key: mapping * value: handlerMethod */ private final Map<T, HandlerMethod> mappingLookup = new LinkedHashMap<>(); /** * key: url * value: list mapping */ private final MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap<>(); /** * key: name * value: handler method */ private final Map<String, List<HandlerMethod>> nameLookup = new ConcurrentHashMap<>(); /** * key:handler method * value: 跨域……
阅读全文
2024年3月6日
Spring HandlerMapping Author: HuiFer 源码阅读仓库: SourceHot-spring 源码路径: org.springframework.jms.annotation.EnableJms org.springframework.web.servlet.HandlerMapping HandlerMapping 处理映射关系, 通过请求转换成对象HandlerExecutionChain 1 2 3 4 public interface HandlerMapping { HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception; // 其他静态变量省略 } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @Override @Nullable public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { // 转换成handler Object handler……
阅读全文