# spring-moon **Repository Path**: TenXZW/spring-moon ## Basic Information - **Project Name**: spring-moon - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-01-29 - **Last Updated**: 2026-02-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Moon 一个轻量级的 Node.js 框架,灵感来自 Spring Framework,支持分层架构和依赖注入。 ## ✨ 特性 - 🚀 TypeScript 支持 - 💉 依赖注入容器(构造器注入) - 🏗️ 分层架构装饰器(@Controller、@Service、@Repository、@Mapper) - 🔄 模块系统和组件扫描 - 🌐 请求上下文管理 - 📦 事务管理 - 🛠️ CLI 脚手架工具 ## 🎯 分层装饰器 类似 Spring Boot 的装饰器系统: | 装饰器 | 用途 | 作用域 | | ---------------- | ---------- | --------- | | `@Controller` | 控制器层 | Request | | `@Service` | 业务逻辑层 | Singleton | | `@Repository` | 数据访问层 | Singleton | | `@Mapper` | 数据映射层 | Singleton | | `@Component` | 通用组件 | Singleton | | `@Configuration` | 配置类 | Singleton | ## 🚀 快速开始 ### 安装依赖并构建 ```bash cd spring-moon pnpm install pnpm run build ``` ### 使用 CLI 创建新项目 ```bash # 直接使用 node packages/spring-moon-cli/dist/index.js create my-app # 或全局安装后使用 pnpm install -g ./packages/spring-moon-cli spring-moon create my-app ``` ### 运行生成的项目 ```bash cd my-app pnpm install pnpm run build pnpm start ``` ## 📋 分层架构示例 ```typescript import { Controller, Service, Repository, Module } from 'spring-moon' // 数据访问层 @Repository() class UserRepository { private users = [ { id: 1, name: 'Alice', email: 'alice@example.com' }, { id: 2, name: 'Bob', email: 'bob@example.com' }, ] findAll() { return this.users } findById(id: number) { return this.users.find(user => user.id === id) } } // 业务逻辑层 @Service() class UserService { constructor(private userRepository: UserRepository) {} getAllUsers() { return this.userRepository.findAll() } getUserById(id: number) { const user = this.userRepository.findById(id) if (!user) { throw new Error(`User with id ${id} not found`) } return user } } // 控制器层 @Controller('/api/users') class UserController { constructor(private userService: UserService) {} async handleGetAllUsers() { return this.userService.getAllUsers() } async handleGetUser(id: number) { return this.userService.getUserById(id) } } // 创建应用模块 const appModule = Module.create({ components: [ UserRepository, // @Repository UserService, // @Service UserController, // @Controller ], }) // 启动应用 async function bootstrap() { const context = appModule.getContext() const requestContext = context.startRequest() try { const userController = appModule.resolve(UserController) const allUsers = await userController.handleGetAllUsers() console.log('All users:', allUsers) const user = await userController.handleGetUser(1) console.log('User 1:', user) console.log(`Controllers: ${appModule.getControllers().length}`) console.log(`Services: ${appModule.getServices().length}`) console.log(`Repositories: ${appModule.getRepositories().length}`) } finally { context.endRequest() } } bootstrap().catch(console.error) ``` ## 许可证 MIT ## 相关链接 - [Gitee 仓库](https://gitee.com/TenXZW/spring-moon) - [问题反馈](https://gitee.com/TenXZW/spring-moon/issues)