tio-boot 整合 jfinal-aop 依赖注入示例
tio-boot 负责应用启动、组件扫描和 HTTP 路由,依赖注入与 AOP 由 jfinal-aop 提供。先用一个最小示例把注入链路跑通,下一章的 Dubbo 示例会直接复用这里的写法。
示例源码位于 https://github.com/litongjava/java-ee-tio-boot-study 的 tio-boot-latest-study/tio-boot-dubbo-2-6-0-demo01 目录:接口在 tio-boot-dubbo-2-6-0-demo01-api 模块,实现类在 -provider 模块,控制器在 -consumer 模块。
添加依赖
xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>nexus.io</groupId>
<artifactId>tio-boot</artifactId>
<version>${tio-boot.version}</version>
</dependency>
<dependency>
<groupId>nexus.io</groupId>
<artifactId>jfinal-aop</artifactId>
<version>${jfinal-aop.version}</version>
</dependency>tio-boot.version 与 jfinal-aop.version 建议在父工程的 properties 中统一声明,各模块只引用属性,升级时改一处即可。
定义接口
java
package nexus.io.tio.dubbo.demo001.service;
public interface HelloService {
String sayHello(String name);
}实现接口
java
package nexus.io.tio.dubbo.demo001.service.impl;
import nexus.io.annotation.AService;
import nexus.io.tio.dubbo.demo001.service.HelloService;
@AService
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}@AService 让 jfinal-aop 在扫描阶段把这个类登记为可注入的组件。
在控制器中获取服务
java
package nexus.io.tio.web.hello.controller;
import nexus.io.annotation.RequestPath;
import nexus.io.jfinal.aop.Aop;
import nexus.io.tio.dubbo.demo001.service.HelloService;
@RequestPath("/")
public class IndexController {
@RequestPath()
public String index() {
HelloService helloService = Aop.get(HelloService.class);
return helloService.sayHello("Tong Li");
}
}控制器由 tio-boot 自行实例化,推荐在控制器里通过 Aop.get(...) 取服务,不要直接 new。
在配置类中注入服务
被 jfinal-aop 管理的配置类可以直接声明字段,由容器完成赋值:
java
package nexus.io.tio.dubbo.demo001.config;
import nexus.io.annotation.AAutowired;
import nexus.io.annotation.AConfiguration;
import nexus.io.annotation.Initialization;
import nexus.io.tio.dubbo.demo001.service.HelloService;
@AConfiguration
public class ServiceAutowiredConfig {
@AAutowired
private HelloService helloService;
@Initialization
public void init() {
System.out.println(helloService.sayHello("Tong Li"));
}
}@Initialization 标注的方法会在应用启动阶段执行,此时依赖已经注入完成。
启动类
java
package nexus.io.tio.dubbo.demo001;
import nexus.io.annotation.AComponentScan;
import nexus.io.tio.boot.TioApplication;
@AComponentScan
public class ProviderApp {
public static void main(String[] args) {
long start = System.currentTimeMillis();
TioApplication.run(ProviderApp.class, args);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
}注解说明
@AComponentScan:tio-boot 的组件扫描入口,指定启动时扫描哪个包下的组件、配置类和控制器。@AService:把实现类登记为可注入的服务组件,作用类似 Spring 的@Service。@AConfiguration:标记配置类,启动时由框架加载。@Initialization:标记配置类中的初始化方法,依赖注入完成后执行。@AAutowired:按类型自动注入字段,用于被 jfinal-aop 管理的 Bean。@RequestPath:tio-boot 的路由注解。标注在类上时定义路径前缀,标注在方法上时定义具体路径;方法未指定路径时使用类上的路径。
注入流程
ProviderApp上的@AComponentScan触发扫描,带@AService的HelloServiceImpl被登记到容器。@AConfiguration的配置类被加载,标注@AAutowired的字段按接口类型找到实现并完成注入。- 控制器里的
Aop.get(HelloService.class)从容器取出同一个单例,无需手动创建实例。 TioApplication.run启动内嵌 HTTP 服务,注册控制器,准备处理请求。
访问结果
启动应用后访问 http://localhost/,请求会路由到 IndexController.index(),返回:
Hello Tong Litio-boot 的默认端口是 80,在 app.properties 中设置 server.port 可以改成其他端口。
下一步
掌握注入写法后,可以继续看 dubbo 2.6.0,那里会把这套注解用到服务提供者和消费者上。
