2026/2/21 0:18:21
网站建设
项目流程
导游网站后台,重庆企业网站推广策略,百度alexa排名,百度公司网站排名怎么做LiteFlow 框架分析系列#xff08;四#xff09;#xff1a;Spring Boot 集成原理
请关注公众号【碳硅化合物AI】
摘要
LiteFlow 对 Spring Boot 的支持可谓是“开箱即用”。你只需要引入 starter 依赖#xff0c;配置好规则文件#xff0c;就能直接在代码里注入 FlowE…LiteFlow 框架分析系列四Spring Boot 集成原理请关注公众号【碳硅化合物AI】摘要LiteFlow 对 Spring Boot 的支持可谓是“开箱即用”。你只需要引入 starter 依赖配置好规则文件就能直接在代码里注入FlowExecutor使用了。这背后发生了什么本篇将深入源码分析 LiteFlow 是如何借力 Spring Boot 的自动装配和生命周期管理的。1. 自动装配入口一切的起点都在liteflow-spring-boot-starter包中。遵循 Spring Boot 的规范spring.factories或 Spring Boot 3 的org.springframework.boot.autoconfigure.AutoConfiguration.imports指向了自动配置类。核心配置类是LiteflowMainAutoConfiguration。ConfigurationAutoConfigureAfter({LiteflowPropertyAutoConfiguration.class})ConditionalOnBean(LiteflowConfig.class)ConditionalOnProperty(prefixliteflow,nameenable,havingValuetrue)Import(SpringAware.class)publicclassLiteflowMainAutoConfiguration{// 1. 注册 FlowExecutorBeanConditionalOnMissingBeanpublicFlowExecutorflowExecutor(LiteflowConfigliteflowConfig,SpringAwarespringAware){FlowExecutorflowExecutornewFlowExecutor();flowExecutor.setLiteflowConfig(liteflowConfig);returnflowExecutor;}// 2. 注册组件扫描器BeanpublicComponentScannercomponentScanner(LiteflowConfigliteflowConfig,SpringAwarespringAware){returnnewComponentScanner(liteflowConfig);}// 3. 注册初始化触发器BeanpublicLiteflowExecutorInitliteflowExecutorInit(FlowExecutorflowExecutor){returnnewLiteflowExecutorInit(flowExecutor);}}这里有三个关键 Bean我们一一解析。2. 组件扫描ComponentScanner你在 Spring Bean 上加了LiteflowComponentLiteFlow 是怎么知道的全靠ComponentScanner。它实现了 Spring 的BeanPostProcessor接口publicclassComponentScannerimplementsBeanPostProcessor{OverridepublicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throwsBeansException{// 获取 Bean 的原始 Class处理被 AOP 代理的情况ClassclazzLiteFlowProxyUtil.getUserClass(bean.getClass());// 判断是否是 LiteFlow 组件检查注解或继承关系// ... (LiteflowScannerProcessStepFactory 逻辑)// 如果是则注册到 LiteFlow 的 FlowBus 中// ...returnbean;}}原理解析当 Spring 容器初始化完一个 Bean 后ComponentScanner会介入检查。如果这个 Bean 是 LiteFlow 的组件它就会提取nodeId、name等信息并将其注册到 LiteFlow 的元数据中心FlowBus里。这也解释了为什么 LiteFlow 的组件可以无缝使用 Spring 的Autowired等特性——因为它们本身就是 Spring 容器管理的 Bean。3. 启动初始化LiteflowExecutorInit规则文件什么时候解析流程链什么时候构建答案是在 Spring 容器启动完成之后。LiteflowExecutorInit实现了SmartInitializingSingleton接口publicclassLiteflowExecutorInitimplementsSmartInitializingSingleton{privatefinalFlowExecutorflowExecutor;OverridepublicvoidafterSingletonsInstantiated(){// 触发 LiteFlow 的初始化流程flowExecutor.init(true);}}afterSingletonsInstantiated方法会在所有单例 Bean 都创建完成之后被调用。这是一个绝佳的时机依赖就绪此时所有的组件 Bean 都已经扫描并创建好了。避免死锁避免在 Bean 创建过程中触发复杂的解析逻辑。调用flowExecutor.init(true)后LiteFlow 开始解析规则文件构建 Chain并将其与之前扫描到的 Node 关联起来最终完成启动。4. 时序图5. 总结LiteFlow 集成 Spring Boot 的思路非常清晰利用 AutoConfiguration自动配置核心组件。利用 BeanPostProcessor抓取用户定义的组件 Bean。利用 SmartInitializingSingleton在 Spring 启动末期触发规则解析。这种深度集成使得开发者几乎感知不到 LiteFlow 的存在专注于写组件逻辑和规则文件即可真正做到了“无侵入”。