文档简要说明
好用的注入和依赖服务框架
基本思想
在一个入口类定义为Application 然后将各种组件通过依赖的方式加载进入内部,然后可以通过自动注入的方式进行组件的调用
基本功能
快速安装
npm install fastcar@core
常用注解引用
ENV 用于指明环境
ApplicationStart 用于在程序启动后调用
ApplicationStop 用于在程序停止前调用
ApplicationRunner 用于声明程序是否在系统中启动
ApplicationInit 用于在程序启动后调用
ApplicationDestory 用于在程序停止前调用
ComponentScan 用于需要扫描的路径
ComponentScanExclusion 用于排除需要扫描的路径
Component 标注为组件
ComponentInjection 自定义组件入口
ComponentScanMust 0.2.58 声明一个必须要扫描注入的方法 防止被笼统的排除在外
Hotter 指定热更新类 0.2.13 版本生效(当我们修改代码后,可以自动热加载文件)
BeanName 指明组件名称(每个组件具有一个系统生成id为"类名:16位随机值",为了便于调用可自定义逻辑名)
Configure 表明为配置组件
Controller 表明为控制组件
Service 表明为服务组件
Repository 表明为依赖的数据组件
Injection 拥有指定注入组件
Application 声明为一个应用
Autowired 自动注入依赖的组件,现在等同为CallDependency
CallDependency 调用时注入组件(调用对象可以不是组件 可以是一个普通类) 0.2.11版本以上生效
AliasInjection 根据别名注入组件
ExceptionMonitor 声明异常监听
Deprecate 用于标注方法是否被放弃 当调用时会提示已放弃该方法
NotImplemented 用于标注方法未实现
Override 用于标注方法已被重新实现
Readonly 作用于属性是否只读
Log 使用logger
AddRequireModule 自定义添加制定木块
AddChildValid 自定义添加校验模块
DefaultVal 标注属性的默认值
NotNull 标注属性不为空
Size 标注属性的大小
Type 标注属性类型
ValidCustom 校验自定义方法
ValidForm 校验表单开启
Rule 校验规则开启
ApplicationSetting 应用内配置设置 具有最高等级
ResourcePath 自定义resource的位置
约定
配置文件约定
- 所有项目配置文件均放在resource文件夹下
- application.yml (或者js,json) 代表应用配置文件
- application-{env}.yml (或者js,json) 代表不同环境的配置 在 application中可以采用env指定开发环境
- application 中 settings 代表第三方自定义组件或者值, application代表应用设置
- 自定义配置可以通过注解Configure来进行映射(后续考虑支持url的配置)
应用程序约定
- 应用程序声明周期分为启动中->运行时->停止前->结束后
- 在介于启动中和运行时的阶段,可采用ApplicationStart进行相应的组件逻辑初始化操作
- 在停止前阶段,可采用ApplicationStop进行相应的组件结束操作
应用程序执行顺序
- 加载系统配置
- 扫描application所在目录或者指定目录下的组件,并注入实例
- 遍历所有实例,并进行装配所依赖的服务
- 运行所有标注为ApplicationStart的组件,而且是按照优先级顺序依次执行
- 服务停止前运行所有标注为ApplicationStop的组件,也是按照优先级顺序依次执行
- 结束运行
声明约定
- index.d.ts 代表实现的 Application及其他配置说明
- annotation.d.ts 代表所实现的注解
- utils.d.ts 代表基础的工具类
基本用法
import { Service } from "@fastcar/core/annotation";
@Service
class HelloService {
say() {
console.info("hello world");
}
}
export default HelloService;
import { Service, BeanName } from "@fastcar/core/annotation";
@Service
@BeanName("HelloService")
class HelloService {
say() {
console.info("hello world");
}
}
export default HelloService;
import { Controller, Autowired } from "@fastcar/core/annotation";
@Controller
class HelloController {
@Autowired
private hello!: HelloService;
callHello() {
this.hello.say();
}
}
import { ApplicationStart } from "@fastcar/core/annotation";
@ApplicationStart()
export default class StartRunner {
run() {
console.info("服务启动后调用的方法");
}
}
@ApplicationRunner
export default class StartRunner {
@ApplicationInit()
run() {
console.info("服务启动后调用的方法");
}
}
import { FastCarApplication } from "@fastcar/core";
import { Application } from "@fastcar/core/annotation";
@Application
@ENV("TEST")
@Log()
class APP {
app!: FastCarApplication;
}
import { NotNull, Size, Rule, ValidForm } from "@fastcar/core/annotation";
class B {
@NotNull
c!: string;
@Size({ minSize: 1, maxSize: 10 })
d?: number;
}
class A {
@ValidForm
test(a: string, @Rule() @NotNull b: B) {
console.log(a, b);
}
}
let instance = new A();
instance.test("a", { c: "c", d: 13 });
import { Configure } from "@fastcar/core/annotation";
@Configure("hello.yml")
class HelloConfig {
hello!: string;
}
@Configure(`evnconfig-${process.env.NODE_ENV}.yml`)
class EnvConfig {
text!: string;
}
import HelloService from "./HelloService";
import { CallDependency } from "@fastcar/core/annotation";
export default class CallService {
@CallDependency
private hello!: HelloService;
sayHello() {
return this.hello.say();
}
}
@Controller
@Hotter
export default class IndexController {
@Autowired
private exampleService!: ExampleService;
@GET("/")
index() {
return Result.ok("hello world");
}
}
import { DataMap } from "@fastcar/core";
describe("数据集合测试", () => {
it("数据集合", () => {
type User = {
uid: number;
name: string;
desc?: {
detail: string;
};
};
let n: DataMap<number, User> = new DataMap();
n.set(1, {
uid: 1,
name: "小明",
});
n.set(2, {
uid: 2,
name: "小王",
desc: {
detail: "住在隔壁",
},
});
let xiaoming = n.get(1);
console.log(xiaoming?.name == "小明");
let oo: { [key: number]: User } = n.toObject();
console.log(oo[1]);
let searchList = n.findByAtts({ uid: 1 });
console.log(searchList);
let searchList2 = n.findByAtts({ "desc.detail": "住在隔壁" });
console.log(searchList2);
let sortList = n.sort([
{
field: "uid",
order: true,
},
]);
console.log(sortList);
});
});
import { ApplicationHook, FastCarApplication, Logger } from "@fastcar/core";
import { Application } from "@fastcar/core/annotation";
@Application
class APP implements ApplicationHook {
app!: FastCarApplication;
@Log("app")
logger!: Logger;
beforeStartServer(): void {
this.logger.debug("beforeStartServer-----");
}
startServer(): void {
this.logger.debug("startServer------");
}
beforeStopServer(): void {
this.logger.debug("beforeStopServer-----");
}
stopServer(): void {
this.logger.debug("stopServer-----");
}
}
更多用法
参考项目git地址 @fastcar/core/test 下的simple内
项目开源地址