Skip to content

Spring Boot Web 整合 Tio Boot ​

本文将详细讲解如何使用 Spring Boot 整合 Tio Boot,实现两者各自优势的互补:

  • 使用 Spring Boot 内置的 Servlet 容器和 Controller 机制
  • 利用 Tio Boot 的其他功能,如数据库操作、AOP 容器以及高性能的服务支持

整合之后的架构可以实现:

  • Tio Boot 框架的代码在 Spring Boot 环境下运行
  • 开发过程中可利用 Tio Boot 快速开发,最终在 Spring Boot 中运行
  • 可利用 Spring Boot 对微服务的支持,将 Tio Boot 作为一个微服务整合到 Spring Cloud 中

注意:由于使用了 Spring Boot 内置的 HTTP Server,因此无法使用 Tio Boot 提供的异步与非阻塞方式处理网络请求。


一、项目依赖配置 ​

在项目的 pom.xml 文件中,需要添加 Spring Boot 和 Tio Boot 相关依赖,同时配置一些必要的插件。下面是示例配置内容:

xml
  <groupId>nexus.io</groupId>
  <artifactId>spring-boot-2.5.6-tio-boot</artifactId>
  <version>1.0</version>
  <properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.5.6</spring-boot.version>

    <hotswap-classloader.version>1.2.9</hotswap-classloader.version>
    <java-model.version>1.1.8</java-model.version>
    <tio-utils.version>3.7.3.v20250201-RELEASE</tio-utils.version>
    <tio-boot.version>2.1.8</tio-boot.version>
    <jfinal-aop.version>1.4.0</jfinal-aop.version>
    <java-db.version>1.6.0</java-db.version>
    <api-table.version>1.5.2</api-table.version>

    <main.class>nexus.io.spring.boot.tio.boot.demo01.Applicaton</main.class>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>


    <!-- SpringBoot集成Test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>

    <!-- 连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <!-- 数据库驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>java-model</artifactId>
      <version>${java-model.version}</version>
    </dependency>

    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>tio-utils</artifactId>
      <version>${tio-utils.version}</version>
    </dependency>

    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>tio-boot</artifactId>
      <version>${tio-boot.version}</version>
    </dependency>

    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>hotswap-classloader</artifactId>
      <version>${hotswap-classloader.version}</version>
    </dependency>


    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>api-table</artifactId>
      <version>${api-table.version}</version>
    </dependency>

    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>jfinal-aop</artifactId>
      <version>${jfinal-aop.version}</version>
    </dependency>

    <dependency>
      <groupId>nexus.io</groupId>
      <artifactId>java-db</artifactId>
      <version>${java-db.version}</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <!-- Spring Boot -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <configuration>
          <includeSystemScope>true</includeSystemScope>
          <!--使该插件支持热启动 -->
          <fork>true</fork>
          <mainClass>${main.class}</mainClass>
          <excludeGroupIds>org.projectlombok</excludeGroupIds>
        </configuration>
        <!-- 设置执行目标 -->
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

依赖说明 ​

  1. properties 部分
    定义了一些全局变量,如 Java 编译版本、Spring Boot 版本以及各个依赖的版本号。通过这种方式可以在整个 pom.xml 中统一管理版本。

  2. dependencies 部分

    • spring-boot-starter-web:创建基于 Spring MVC 的 Web 应用。
    • lombok:通过注解生成 getter、setter 等,简化代码。
    • spring-boot-starter-test 和 junit:用于测试阶段的依赖。
    • tio-boot:高性能 Java Web 开发框架。
    • java-db、druid、mysql-connector-java 等:用于数据库连接、监控和驱动支持。
    • 其他依赖(如 hotswap-classloader、tio-utils、jfinal-aop、api-table)提供了 Tio Boot 及相关组件的支持。
  3. build 部分
    配置了 spring-boot-maven-plugin 插件,用于打包成可执行 JAR 文件,支持热启动,并指定了应用的主类。

  4. dependencyManagement 部分
    引入了 spring-boot-dependencies,用于集中管理项目中所有依赖的版本,避免版本冲突。


二、配置文件设置 ​

在 src/main/resources 目录下创建 app.properties 文件,用于配置 Tio Boot 的一些全局参数:

properties
server.listening.enable=false

参数 server.listening.enable=false 的作用是告诉 TioApplication 不启动 Tio Boot 自带的 HTTP 服务器,这样项目中只会启动 Spring Boot 的服务器。


三、启动类编写 ​

下面是一个 Spring Boot 应用的启动类示例,同时启动了 Spring Boot 和 Tio Boot 应用。

java
package nexus.io.spring.boot.tio.boot.demo01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import nexus.io.annotation.AComponentScan;
import nexus.io.tio.boot.TioApplication;

@SpringBootApplication
@AComponentScan
public class SpringBootWebApplication {

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    SpringApplication.run(SpringBootWebApplication.class, args);
    TioApplication.run(SpringBootWebApplication.class, args);
    long end = System.currentTimeMillis();
    System.out.println(end - start + "(ms)");
  }
}

说明:

  • @SpringBootApplication 是 Spring Boot 的入口注解,包含自动配置和组件扫描。
  • @AComponentScan 用于 Tio Boot 的包扫描。
  • 在 main 方法中,先启动 Spring Boot,再启动 Tio Boot,同时计算并打印整个启动过程的耗时。

四、数据库配置 ​

在整合项目中,需要配置数据库连接和 ORM 插件。本例中使用了 Druid 数据库连接池和 ActiveRecordPlugin 进行数据库操作。新建配置类 ActiveRecordPluginConfig,内容如下:

java
package nexus.io.spring.boot.tio.boot.demo01.config;

import nexus.io.annotation.ABean;
import nexus.io.annotation.AConfiguration;
import nexus.io.db.activerecord.ActiveRecordPlugin;
import nexus.io.db.activerecord.OrderedFieldContainerFactory;
import nexus.io.db.druid.DruidPlugin;
import nexus.io.jfinal.aop.Aop;

@AConfiguration
public class ActiveRecordPluginConfig {
  @ABean(priority = 10)
  public DruidPlugin druidPlugin() {
    String jdbcUrl = "jdbc:mysql://192.168.3.9:3306/mybatis_plus_study";
    String jdbcUser = "root";
    String jdbcPswd = "robot_123456#";

    DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, jdbcUser, jdbcPswd);
    druidPlugin.start();
    return druidPlugin;
  }

  @ABean
  public ActiveRecordPlugin activeRecordPlugin() {
    DruidPlugin druidPlugin = Aop.get(DruidPlugin.class);
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
    arp.setContainerFactory(new OrderedFieldContainerFactory());
    arp.start();
    return arp;
  }
}

说明:

  • 使用 @AConfiguration 标识该类为 Tio Boot 的配置类。
  • druidPlugin() 方法创建并启动 Druid 连接池,并通过 @ABean(priority = 10) 确保其在其他 Bean 之前初始化。
  • activeRecordPlugin() 方法从 Tio Boot 的 AOP 容器中获取 DruidPlugin 实例,并配置 ActiveRecordPlugin 用于数据库操作。

五、业务逻辑实现 ​

1. 定义 Service 接口 ​

首先创建 UserService 接口,用于定义获取用户列表的方法:

java
package nexus.io.spring.boot.tio.boot.demo01.services;

import java.util.List;

import nexus.io.db.activerecord.Row;

public interface UserService {
  List<Row> listAll();
}

2. 实现 Service 接口 ​

接下来创建 UserServiceImpl 类,实现上述接口,通过调用数据库操作获取用户列表。注意使用了 Tio Boot 的 @AService 注解,将该类纳入 Tio Boot 的管理范围。

java
package nexus.io.spring.boot.tio.boot.demo01.services;

import java.util.List;

import nexus.io.annotation.AService;
import nexus.io.db.activerecord.Db;
import nexus.io.db.activerecord.Row;

@AService
public class UserServiceImpl implements UserService {

  public List<Row> listAll() {
    List<Row> all = Db.findAll("user");
    return all;
  }
}

3. 编写 Controller ​

在 Controller 中调用 UserService 的方法,并返回查询到的用户数据。该 Controller 采用 Spring Boot 的 @RestController 注解,同时在方法中通过 Tio Boot 的 AOP 容器获取 UserService 的实例。

java
package nexus.io.spring.boot.tio.boot.demo01.controller;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import nexus.io.db.activerecord.Row;
import nexus.io.jfinal.aop.Aop;
import nexus.io.spring.boot.tio.boot.demo01.services.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

  @RequestMapping("/listAll")
  public List<Row> listAll() {
    return Aop.get(UserService.class).listAll();
  }
}

测试访问:
启动项目后,访问接口:http://localhost:10510/user/listAll 即可查看返回的用户数据。


六、打包与运行 ​

1. 打包成 fastjar ​

在项目根目录下执行以下 Maven 命令进行打包,跳过测试并略过 GPG 签名:

bash
mvn clean install -DskipTests -Dgpg.skip

2. 启动 fastjar ​

打包完成后,使用下面的命令启动生成的 fastjar 包:

bash
java -jar target\spring-boot-2.5.6-tio-boot-1.0.jar

再次访问:http://localhost:10510/user/listAll 进行接口测试。


七、编写测试代码 ​

为了验证 UserService 的功能,可以编写测试类 UserServiceTest,使用 JUnit 进行测试。测试类示例如下:

java
package nexus.io.spring.boot.tio.boot.demo01.services;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import nexus.io.db.activerecord.Row;
import nexus.io.jfinal.aop.Aop;
import nexus.io.spring.boot.tio.boot.demo01.SpringBootWebApplication;
import nexus.io.tio.boot.testing.TioBootTest;

public class UserServiceTest {

  @AopBefore
  public void before() {
    TioBootTest.run(SpringBootWebApplication.class);
  }

  @Test
  public void test() {
    UserService userService = Aop.get(UserService.class);
    List<Row> listAll = userService.listAll();
    System.out.println(listAll);
  }

}

说明:

  • 使用 @AopBefore 注解的方法会在每个测试方法执行前启动 TioApplication(通过传递 --tio.noServer=true 参数,不启动 Tio Boot 的 HTTP 服务)。
  • @Test 方法中,通过 AOP 容器获取 UserService 实例,并调用 listAll() 方法打印查询结果。

八、完整代码仓库 ​

本地示例的所有代码均已上传至 GitHub,欢迎前往查看和学习:

GitHub 代码仓库

MIT Licensed