# SPeed-Spring 手写一个基于 SPeed-Tomcat 的简单 Spring 框架 **Repository Path**: tlgen_1/speed-spring ## Basic Information - **Project Name**: SPeed-Spring 手写一个基于 SPeed-Tomcat 的简单 Spring 框架 - **Description**: 1.该 Speed-Spring 框架是依赖自己手写的 Speed-Tomcat, 内嵌 tomcat 服务器运行 2.支持全局的启动类, 通过调用 SpeedSpringApplication.run(args) 来运行项目 3.支持 yml 文件配置, 配置启动端口、属性值 4.支持 config、component、model、service、impl、controller 编程 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-02-01 - **Last Updated**: 2025-04-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SPeed-Spring 手写 Spring 框架 #### 框架简介 1.该 Speed-Spring 框架是依赖自己手写的 Speed-Tomcat, 内嵌 tomcat 服务器运行 2.支持全局的启动类, 通过调用 SpeedSpringApplication.run(args) 来运行项目 3.支持 yml 文件配置, 配置启动端口、属性值 4.支持 config、component、model、service、impl、controller 编程模型, 与 Spring 一样正常玩转调用 5.支持 @Configuration、@Bean、@Component、@Service、@RestController 注解来注册 bean 6.支持 @Value 绑定来自配置文件中的属性值 7.支持 @Autowired 注入其他的 bean 类型 #### 快速上手 可以直接下载代码根目录下的 [speed-spring-0.0.1.jar](speed-spring-0.0.1.jar) 引入, 或本地 install 引入如下(未打入 Maven 中央仓库) ```ruby com.tlgen speed-spring 0.0.1 ``` #### 新建一个 spring-test 项目, 纯 maven 项目 ![1](doc/1.png) #### 引入 speed-spring 依赖 ```ruby com.tlgen speed-spring 0.0.1 ``` #### 准备一个启动类 MySpringApplication.java ```ruby package org.example; import com.tlgen.spring.annotation.SpeedBootApplication; import com.tlgen.spring.utils.SpeedSpringApplication; @SpeedBootApplication public class MySpringApplication { public static void main(String[] args) { SpeedSpringApplication.run(args); } } ``` #### 准备一个 yml 文件配置 application.yml ```ruby server: port: 8890 ``` 这样子简单准备之后, 该 spring-test 的项目可以直接启动成功了 #### 测试一个 HelloController ```ruby package org.example.controller; import com.tlgen.spring.annotation.RestController; import com.tlgen.spring.annotation.Value; import com.tlgen.tomcat.annotation.GetMapping; @RestController public class HelloController { @Value("${hello.custom.myKey}") private String myKey; @GetMapping("/hello") public String hello() { System.out.println("打印一下 myKey 的值: " + myKey); return "SUCCESS"; } } ``` #### 同时在 application.yml 中添加自定义配置 ```ruby hello: custom: my-key: abc123 ``` #### 调用 hello 接口测试 http://localhost:8890/hello 返回 SUCCESS #### 查看控制台日志 ![5](doc/5.png) 为了能够实现跟 spring 框架一样编程模型, 可以测试模仿下, 看能不能正常使用 #### 准备一个模型类 SysUser.java ```ruby package org.example.model; import lombok.Data; @Data public class SysUser { private String id; private String username; private String mobile; private String email; private String gender; } ``` #### 准备一个对应模型类的接口类 SysUserService.java ```ruby package org.example.service; import org.example.model.SysUser; import java.util.List; public interface SysUserService { List selectList(); } ``` #### 准备一个接口类对应的实现类 SysUserServiceImpl.java ```ruby package org.example.service.impl; import com.tlgen.spring.annotation.Autowired; import com.tlgen.spring.annotation.Value; import org.example.model.SysUser; import org.example.service.AccountService; import org.example.service.SysUserService; import java.util.ArrayList; import java.util.List; @Service public class SysUserServiceImpl implements SysUserService { @Value("${pay.account.username}") private String accountName; @Autowired private AccountService accountService; @Override public List selectList() { System.out.println("accountName 的值: " + accountName); System.out.println("accountService 的值: " + accountService); String computedResult = accountService.computedByName(accountName); System.out.println("调用 accountService.computedByName(accountName) 返回的结果: " + computedResult); List userList = new ArrayList<>(); SysUser user = new SysUser(); user.setId("1"); user.setUsername("小e"); user.setMobile("137"); user.setEmail("1@qq.com"); userList.add(user); return userList; } } ``` #### 准备一个被调用的业务类接口 AccountService.java ```ruby package org.example.service; public interface AccountService { String computedByName(String accountName); } ``` #### 准备一个被调用的业务实现类 AccountServiceImpl.java ```ruby package org.example.service.impl; import com.tlgen.spring.annotation.Service; import org.example.service.AccountService; @Service public class AccountServiceImpl implements AccountService { @Override public String computedByName(String accountName) { return "计算成功!"; } } ``` #### 准备一个调用接口的控制类 SysUserController.java ```ruby package org.example.controller; import com.tlgen.spring.annotation.Autowired; import com.tlgen.spring.annotation.RestController; import com.tlgen.tomcat.annotation.GetMapping; import org.example.model.SysUser; import org.example.service.SysUserService; import java.util.List; @RestController public class SysUserController { @Autowired SysUserService userService; @GetMapping("/selectList") public List selectList() { System.out.println("userService 的值: " + userService); return userService.selectList(); } } ``` #### 准备自定义一些配置 当然还要准备一些我们用打的配置属性, 在 application.yml 中添加 ```ruby pay: account: id: abc16672 username: wxUser123 mobile: 136xxx ``` #### 启动 spring-test 项目 ![2](doc/2.png) #### 调用 selectList 接口测试 http://localhost:8890/selectList 返回: [{"email":"1@qq.com","id":"1","mobile":"137","username":"小e"}] #### 查看控制台日志 ![4](doc/4.png) #### 使用 @Configuration 注解定义 bean ```ruby package org.example.config; import com.tlgen.spring.annotation.Bean; import com.tlgen.spring.annotation.Configuration; import org.example.model.Apple; @Configuration public class MyTestConfig { @Bean Apple apple() { return new Apple("2", 260, 300); } } ``` #### 使用 @Bean 注解定义 bean ```ruby @Bean Apple apple() { return new Apple("2", 260, 300); } ``` #### 使用 @Component 注解定义 bean ```ruby package org.example.component; import com.tlgen.spring.annotation.Component; @Component public class MyTestComponent { } ``` #### 使用 @Service 注解定义 bean ```ruby package org.example.service.impl; import com.tlgen.spring.annotation.Service; import org.example.service.AccountService; @Service public class AccountServiceImpl implements AccountService { @Override public String computedByName(String accountName) { return "计算成功!"; } } ``` #### 使用 @RestController 注解定义 bean ```ruby package org.example.controller; import com.tlgen.spring.annotation.RestController; import com.tlgen.tomcat.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "SUCCESS"; } } ``` #### 使用 @Autowired 注解引入 bean ```ruby package org.example.controller; import com.tlgen.spring.annotation.Autowired; import com.tlgen.spring.annotation.RestController; import com.tlgen.tomcat.annotation.GetMapping; import org.example.model.SysUser; import org.example.service.SysUserService; import java.util.List; @RestController public class SysUserController { @Autowired private SysUserService userService; @GetMapping("/selectList") public List selectList() { System.out.println("userService 的值: " + userService); return userService.selectList(); } } ``` #### 使用 @Value 注解绑定属性值 ```ruby package org.example.service.impl; import com.tlgen.spring.annotation.Autowired; import com.tlgen.spring.annotation.Value; import org.example.model.SysUser; import org.example.service.AccountService; import org.example.service.SysUserService; import java.util.ArrayList; import java.util.List; public class SysUserServiceImpl implements SysUserService { @Value("${pay.account.username}") private String accountName; @Autowired private AccountService accountService; @Override public List selectList() { System.out.println("accountName 的值: " + accountName); System.out.println("accountService 的值: " + accountService); String computedResult = accountService.computedByName(accountName); System.out.println("调用 accountService.computedByName(accountName) 返回的结果: " + computedResult); List userList = new ArrayList<>(); SysUser user = new SysUser(); user.setId("1"); user.setUsername("小e"); user.setMobile("137"); user.setEmail("1@qq.com"); userList.add(user); return userList; } } ``` ```ruby package org.example.controller; import com.tlgen.spring.annotation.RestController; import com.tlgen.spring.annotation.Value; import com.tlgen.tomcat.annotation.GetMapping; @RestController public class HelloController { @Value("${hello.custom.myKey}") private String myKey; @GetMapping("/hello") public String hello() { System.out.println("打印一下 myKey 的值: " + myKey); return "SUCCESS"; } } ``` #### 获取所有 bean 对象实例 ![6](doc/6.png) #### 看个多示例 ```ruby package org.example.controller; import com.tlgen.spring.annotation.Autowired; import com.tlgen.spring.annotation.Bean; import com.tlgen.spring.annotation.RestController; import com.tlgen.spring.annotation.Value; import com.tlgen.spring.utils.SpeedSpringContextUtils; import com.tlgen.tomcat.annotation.GetMapping; import org.example.model.Account; import org.example.model.SysUser; import org.example.service.AccountService; import org.example.service.SysUserService; import java.util.List; @RestController public class HelloController { @Value("${pay.account.username}") private String username; @Autowired private AccountService accountService; @Autowired private SysUserService userService; @Bean Account account() { return new Account("1", "100", "A123456", "账户名称测试"); } @GetMapping("/test") public String test() { System.out.println("打印一下 username 的值: " + username); System.out.println("打印一下 accountService 的值: " + accountService); System.out.println("打印一下 userService 的值: " + userService); System.out.println("测试调用 bean"); String s = accountService.computedByName(username); List userList = userService.selectList(); System.out.println("调用 accountService.computedByName(username) 方法返回的结果: " + s); System.out.println("调用 userService.selectList() 方法返回的结果: " + userList); Account account = SpeedSpringContextUtils.getBean(Account.class); System.out.println("打印一下 account 的值: " + account); return "SUCCESS"; } } ``` #### 定义的 Account 实体类 ```ruby package org.example.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Account { private String id; private String userId; private String accountNo; private String accountName; } ``` #### 测试调用结果 http://localhost:8890/test 返回 SUCCESS #### 查看控制台日志 ![7](doc/7.png)