Skip to content

feat: Spring 风格 DI 模块深度优化与框架完善 - #2

Open
xiusin wants to merge 4 commits into
mainfrom
trae/solo-agent-qMKrVV
Open

feat: Spring 风格 DI 模块深度优化与框架完善#2
xiusin wants to merge 4 commits into
mainfrom
trae/solo-agent-qMKrVV

Conversation

@xiusin

@xiusin xiusin commented Jun 22, 2026

Copy link
Copy Markdown
Owner

概述

参考 Spring 框架对 DI 模块及整个框架进行深度优化,实现合理的功能、正确的逻辑、易用的 API。

主要变更

DI 模块(Spring 风格重写)

  • 新增 BeanScope 枚举(singleton / prototype / request)三作用域
  • 新增 BeanDefinition 结构(name/type/scope/factory/init/deinit/lazy/primary)
  • 新增 Bean 结构(definition + instance + initialized 标志)
  • Container 提供 register_singleton / register_factory / register_lazy / bind_instance API
  • Container.get[T] 带类型校验,不匹配返回明确错误
  • create_request_container() 实现 request 作用域子容器隔离
  • 保留旧 API(Builder / Service / inject_on / get[T])作为兼容别名

框架集成

  • Application 持有独立 Container(不再使用全局默认)
  • 新增 app.register_singleton / register_factory / register_lazy / bind_instance
  • Context.di[T] 先查 request 容器再查 root 容器
  • handle 中受 enable_request_scope 开关控制创建 request 子容器
  • mount[T] 支持接口类型字段注入,去除平台分支,统一 C.memcpy 指针赋值

事件模块(新增 event/

  • Event 接口 + EventListener 类型 + EventBus(线程安全 register/emit/off)
  • 内置事件:ServerStartEvent / ServerShutdownEvent / RequestStartEvent / RequestEndEvent
  • app.run() emit ServerStart,graceful_shutdown emit ServerShutdown,每个请求 emit RequestStart/RequestEnd

会话优化

  • 抽象 SessionStore 接口(get/set/delete/exists/gc)
  • MemorySessionStore 作为默认内存实现
  • Session 持有 &MemorySessionStore,所有读写走 store
  • 修复 get 过期逻辑(返回空 map 且 exists=false)
  • app.set_session_store 可替换存储

验证器优化

  • Required 支持 int/i64/u64/f64/bool
  • Min/Max 补全 f64 字段分支
  • Number 对数值类型直接返回 ok
  • 修复 validate 中 rule 解析的 mut 覆盖隐患

配置扩展

  • 新增 profileenable_request_scopemax_request_scope_size 字段

关键 Bug 修复

  • 移除 v.reflection 依赖(运行时 array.first: array is empty 崩溃的根因),改用 V comptime 特性
  • 修复 pool.v 泛型闭包导致的 C 编译错误,改用 call_factory_voidptr 辅助方法绕过 V 0.5.1 的 !&T 返回类型 bug
  • 修复 middleware/compress.vheader.get() 结果传播

文档与示例

  • README 完整重写:新增 DI、事件系统、会话管理、配置等章节
  • examples/example.v 演示事件监听与新 DI 用法

验证结果

  • v test . → 4 passed, 4 total
  • v build-module . → 成功
  • v -W build-module examples/example.v → 无错误无警告

测试覆盖

  • di_test.v:singleton / prototype / lazy / 接口绑定 / 类型校验 / request 隔离 / 父容器委托 / has-remove-clear / 兼容 inject_on / factory singleton
  • event_test.v:基本注册分发 / 多监听器 / off / off_all / 内置事件 / emit 内置事件 / 无监听器 / 监听器错误传播
  • validate_test.v:required int/f64/bool / number int / min-max f64 / 未知验证器
  • pool_test.v:对象池 acquire/release 回收复用

xiusin and others added 4 commits June 21, 2026 18:59
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
- pool.v: 移除泛型闭包包装器修复 C 编译错误,改用 call_factory_voidptr 辅助方法绕过 V 0.5.1 的 !&T 返回类型 bug
- app.v: 移除 v.reflection 导入(运行时 panic 的根因),改用 comptime 特性($if field.typ is Context、$if T !is $interface、field.indirections)
- di/builder.v: 同步移除 v.reflection,inject_on 接口检查改用 comptime
- middleware/compress.v: 修复 header.get() 结果传播
- examples/example.v: 新增事件监听演示,修复 text() 返回值传播
- README.md: 完整重写,新增 DI、事件系统、会话管理、配置等章节
- checklist.md / tasks.md: 自审全部勾选完成
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant