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……
阅读全文
2024年3月6日
Spring MessageConverter Author: HuiFer 源码阅读仓库: SourceHot-spring 源码路径: org.springframework.messaging.converter.MessageConverter MessageConverter 消息转换接口 类图如下 两个方法 fromMessage: 从消息转换到 Object 1 Object fromMessage(Message<?> message, Class<?> targetClass); toMessage: 从 Object 转换到消息 1 Message<?> toMessage(Object payload, @Nullable MessageHeaders headers); 序号 class 作用 1 ByteArrayMessageConverter byte 数组消息转换器 2 MappingJackson2MessageConverter jackson2 的消息转换器 3 MarshallingMessageConverter xml 的消息转换器 4 StringMessageConverter 字符串消息转换器 AbstractMessageConverter 类图: fromMessage 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Override @Nullable public final Object fromMessage(Message<?> message,……
阅读全文
2024年3月6日
Spring JmsTemplate Author: HuiFer 源码阅读仓库: SourceHot-spring 源码路径: org.springframework.jms.core.JmsTemplate 源码分析 send 发送消息 1 2 3 4 5 6 7 8 9 @Override public void send(final String destinationName, final MessageCreator messageCreator) throws JmsException { // 执行. execute(session -> { Destination destination = resolveDestinationName(session, destinationName); doSend(session, destination, messageCreator); return null; }, false); } 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 @Nullable public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException { Assert.notNull(action, "Callback object must not be null"); Connection conToClose = null; Session sessionToClose = null; try { Session sessionToUse = ConnectionFactoryUtils.doGetTransactionalSession( obtainConnectionFactory(), this.transactionalResourceFactory, startConnection); if (sessionToUse ==……
阅读全文
2024年3月6日
Spring EnableJms 注解 Author: HuiFer 源码阅读仓库: SourceHot-spring 源码路径: org.springframework.jms.annotation.EnableJms 源码分析 1 2 3 4 5 6 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(JmsBootstrapConfiguration.class) public @interface EnableJms { } 该类的切入点在@Import(JmsBootstrapConfiguration.class) , 直接看JmsBootstrapConfiguration就可以了 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21……
阅读全文