@fastcar/core 0.2.63 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/annotation.d.ts +3 -0
- package/index.d.ts +57 -50
- package/package.json +4 -4
- package/src/FastCarApplication.ts +184 -123
- package/src/annotation/Application.ts +3 -1
- package/src/annotation/bind/AddInjectionService.ts +17 -0
- package/src/annotation/bind/AddRequireModule.ts +1 -1
- package/src/annotation/bind/AliasInjection.ts +8 -15
- package/src/annotation/bind/Autowired.ts +1 -1
- package/src/annotation/bind/CallDependency.ts +7 -17
- package/src/annotation/bind/DemandInjection.ts +16 -0
- package/src/annotation/stereotype/Log.ts +13 -9
- package/src/annotation/stereotype/Value.ts +5 -0
- package/src/annotation.ts +2 -0
- package/src/constant/FastCarMetaData.ts +2 -0
- package/src/model/WinstonLogger.ts +1 -1
- package/src/type/ClassConstructor.ts +5 -0
- package/src/type/ComponentDesc.ts +13 -0
- package/src/type/FileHotterDesc.ts +6 -0
- package/src/utils/ClassLoader.ts +2 -1
- package/src/utils/ValidationUtil.ts +1 -1
- package/target/FastCarApplication.js +764 -708
- package/target/annotation/Application.js +46 -45
- package/target/annotation/ExceptionMonitor.js +16 -16
- package/target/annotation/bind/AddInjectionService.js +16 -0
- package/target/annotation/bind/AddRequireModule.js +21 -21
- package/target/annotation/bind/AliasInjection.js +19 -23
- package/target/annotation/bind/Autowired.js +13 -13
- package/target/annotation/bind/CallDependency.js +16 -23
- package/target/annotation/bind/DemandInjection.js +15 -0
- package/target/annotation/data/DBType.js +11 -11
- package/target/annotation/data/DS.js +54 -54
- package/target/annotation/data/DSIndex.js +9 -9
- package/target/annotation/data/Entity.js +10 -10
- package/target/annotation/data/Field.js +18 -18
- package/target/annotation/data/PrimaryKey.js +9 -9
- package/target/annotation/data/SqlSession.js +9 -9
- package/target/annotation/data/Table.js +35 -35
- package/target/annotation/data/Transactional.js +52 -52
- package/target/annotation/env/ApplicationSetting.js +25 -25
- package/target/annotation/env/BaseFilePath.js +14 -14
- package/target/annotation/env/BaseName.js +9 -9
- package/target/annotation/env/BasePath.js +14 -14
- package/target/annotation/env/ENV.js +10 -10
- package/target/annotation/env/ResourcePath.js +9 -9
- package/target/annotation/lifeCycle/AddLifeCycleItem.js +17 -18
- package/target/annotation/lifeCycle/ApplicationDestory.js +15 -15
- package/target/annotation/lifeCycle/ApplicationInit.js +15 -15
- package/target/annotation/lifeCycle/ApplicationRunner.js +7 -7
- package/target/annotation/lifeCycle/ApplicationStart.js +24 -24
- package/target/annotation/lifeCycle/ApplicationStop.js +20 -20
- package/target/annotation/property/Deprecate.js +19 -19
- package/target/annotation/property/NotImplemented.js +8 -8
- package/target/annotation/property/Override.js +7 -7
- package/target/annotation/property/Readonly.js +16 -16
- package/target/annotation/scan/ComponentInjection.js +21 -21
- package/target/annotation/scan/ComponentScan.js +9 -9
- package/target/annotation/scan/ComponentScanExclusion.js +25 -25
- package/target/annotation/scan/ComponentScanMust.js +25 -25
- package/target/annotation/scan/Hotter.js +8 -8
- package/target/annotation/stereotype/BeanName.js +11 -11
- package/target/annotation/stereotype/Component.js +8 -8
- package/target/annotation/stereotype/Configure.js +14 -14
- package/target/annotation/stereotype/Controller.js +9 -9
- package/target/annotation/stereotype/Injection.js +12 -12
- package/target/annotation/stereotype/Log.js +19 -17
- package/target/annotation/stereotype/Repository.js +9 -9
- package/target/annotation/stereotype/Service.js +9 -9
- package/target/annotation/stereotype/Value.js +5 -0
- package/target/annotation/valid/AddChildValid.js +56 -56
- package/target/annotation/valid/CustomType.js +11 -11
- package/target/annotation/valid/DefaultVal.js +10 -10
- package/target/annotation/valid/NotNull.js +8 -8
- package/target/annotation/valid/Rule.js +99 -100
- package/target/annotation/valid/Size.js +10 -10
- package/target/annotation/valid/Type.js +10 -10
- package/target/annotation/valid/ValidCustom.js +17 -17
- package/target/annotation/valid/ValidForm.js +133 -133
- package/target/annotation.js +110 -108
- package/target/config/ApplicationConfig.js +2 -2
- package/target/config/SysConfig.js +19 -19
- package/target/constant/AppStatusEnum.js +9 -9
- package/target/constant/BootPriority.js +11 -11
- package/target/constant/CommonConstant.js +18 -18
- package/target/constant/ComponentKind.js +11 -11
- package/target/constant/DataTypes.js +19 -19
- package/target/constant/FastCarMetaData.js +33 -31
- package/target/constant/LifeCycleModule.js +9 -9
- package/target/db.js +49 -49
- package/target/index.js +21 -21
- package/target/interface/ApplicationHook.js +2 -2
- package/target/interface/ApplicationInterface.js +2 -2
- package/target/interface/ApplicationRunnerService.js +2 -2
- package/target/interface/DataSourceManager.js +2 -2
- package/target/interface/Logger.js +5 -5
- package/target/model/BaseMapper.js +103 -98
- package/target/model/DataMap.js +87 -87
- package/target/model/FormRuleModel.js +2 -2
- package/target/model/ValidError.js +5 -5
- package/target/model/WinstonLogger.js +98 -96
- package/target/type/ClassConstructor.js +2 -0
- package/target/type/ComponentDesc.js +9 -2
- package/target/type/DesignMeta.js +15 -15
- package/target/type/FileHotterDesc.js +9 -2
- package/target/type/MapperType.js +2 -2
- package/target/type/ProcessType.js +2 -2
- package/target/type/SqlError.js +5 -5
- package/target/type/WinstonLoggerType.js +9 -9
- package/target/utils/ClassLoader.js +66 -65
- package/target/utils/ClassUtils.js +35 -35
- package/target/utils/CryptoUtil.js +86 -86
- package/target/utils/DataFormat.js +88 -88
- package/target/utils/DateUtil.js +71 -71
- package/target/utils/FileUtil.js +153 -153
- package/target/utils/FormatStr.js +14 -14
- package/target/utils/IPUtils.js +34 -34
- package/target/utils/Id.js +8 -9
- package/target/utils/Mix.js +62 -62
- package/target/utils/ReflectUtil.js +22 -22
- package/target/utils/TypeUtil.js +53 -53
- package/target/utils/ValidationUtil.js +123 -123
- package/target/utils.js +25 -25
- package/test/example/logs/fastcar-server.app.log +0 -0
- package/test/example/logs/fastcar-server.sys.log +10 -0
- package/test/example/logs/fastcar-server.sys1.log +8 -0
- package/test/example/logs/fastcar-server.sys2.log +11 -0
- package/test/example/logs/fastcar-server.sys3.log +0 -0
- package/test/example/logs/other-server.app.log +0 -0
- package/test/example/logs/other-server.sys.log +5 -0
- package/test/example/logs/sys.log +0 -0
- package/test/example/logs/sys1.log +0 -0
- package/test/example/resource/application.yml +2 -0
- package/test/example/resource/hello.yml +1 -1
- package/test/example/simple/app-test.ts +2 -1
- package/test/example/simple/app.ts +16 -16
- package/test/example/simple/service/CallService.ts +2 -0
- package/test/example/simple/service/HelloService.ts +2 -0
- package/test/unit/sys2.log +6 -0
- package/test/unit/sys3.log +6 -0
- package/test/unit/sys4.log +6 -0
- package/test/unit/sys5.log +6 -0
- package/test/unit/sys6.log +6 -0
|
@@ -19,17 +19,19 @@ import Component from "./annotation/stereotype/Component";
|
|
|
19
19
|
import WinstonLogger from "./model/WinstonLogger";
|
|
20
20
|
import * as winston from "winston";
|
|
21
21
|
import Logger from "./interface/Logger";
|
|
22
|
-
import { ComponentDesc } from "./type/ComponentDesc";
|
|
22
|
+
import { ComponentDesc, InjectionMeta } from "./type/ComponentDesc";
|
|
23
23
|
import DateUtil from "./utils/DateUtil";
|
|
24
24
|
import { ProcessType } from "./type/ProcessType";
|
|
25
|
-
import { FileHotterDesc } from "./type/FileHotterDesc";
|
|
25
|
+
import { FileHotterDesc, HotReloadEnum } from "./type/FileHotterDesc";
|
|
26
26
|
import { LifeCycleType } from "./annotation/lifeCycle/AddLifeCycleItem";
|
|
27
27
|
import { WinstonLoggerType } from "./type/WinstonLoggerType";
|
|
28
|
-
import
|
|
28
|
+
import { ClassConstructor } from "./type/ClassConstructor";
|
|
29
|
+
import ReflectUtil from "./utils/ReflectUtil";
|
|
30
|
+
import { Log } from "./annotation";
|
|
29
31
|
|
|
30
32
|
@Component
|
|
31
33
|
class FastCarApplication extends Events {
|
|
32
|
-
protected componentMap: Map<string | symbol,
|
|
34
|
+
protected componentMap: Map<string | symbol, ClassConstructor<Object> | Object>; //组件键值对
|
|
33
35
|
protected sysConfig: SYSConfig; //系统配置
|
|
34
36
|
protected basePath!: string; //入口文件夹路径
|
|
35
37
|
protected baseFileName!: string; //入口文件路径
|
|
@@ -37,16 +39,17 @@ class FastCarApplication extends Events {
|
|
|
37
39
|
protected applicationStatus: AppStatusEnum;
|
|
38
40
|
|
|
39
41
|
@Log("sys")
|
|
40
|
-
protected sysLogger!:
|
|
42
|
+
protected sysLogger!: Logger;
|
|
41
43
|
|
|
42
44
|
protected componentDeatils: Map<string | symbol, ComponentDesc>; //读取路径 名称
|
|
43
45
|
protected liveTime: number;
|
|
44
46
|
protected watchFiles: Map<string, FileHotterDesc[]>;
|
|
45
47
|
protected resourcePath: string = ""; //资源路径
|
|
46
|
-
protected delayHotIds: Map<string, { fp: string; loadType:
|
|
48
|
+
protected delayHotIds: Map<string, { fp: string; loadType: HotReloadEnum }>;
|
|
47
49
|
protected reloadTimerId: NodeJS.Timeout | null;
|
|
48
50
|
protected basename: string = CommonConstant.Application;
|
|
49
51
|
protected componentAliasMap: Map<string | symbol, string | symbol>;
|
|
52
|
+
protected hotConfigure: Map<string, string[]>;
|
|
50
53
|
|
|
51
54
|
constructor() {
|
|
52
55
|
super();
|
|
@@ -60,6 +63,7 @@ class FastCarApplication extends Events {
|
|
|
60
63
|
this.delayHotIds = new Map();
|
|
61
64
|
this.reloadTimerId = null;
|
|
62
65
|
this.componentAliasMap = new Map();
|
|
66
|
+
this.hotConfigure = new Map();
|
|
63
67
|
|
|
64
68
|
this.loadSelf();
|
|
65
69
|
this.addHot();
|
|
@@ -81,6 +85,7 @@ class FastCarApplication extends Events {
|
|
|
81
85
|
id: key,
|
|
82
86
|
name: "FastCarApplication",
|
|
83
87
|
path: __filename,
|
|
88
|
+
classZ: this,
|
|
84
89
|
});
|
|
85
90
|
//暴露一个全局的app 以便调用
|
|
86
91
|
Reflect.set(global, CommonConstant.FastcarApp, this);
|
|
@@ -91,22 +96,26 @@ class FastCarApplication extends Events {
|
|
|
91
96
|
* @version 1.1 热更新配置文件
|
|
92
97
|
*/
|
|
93
98
|
addHot() {
|
|
94
|
-
this.on(
|
|
99
|
+
this.on(HotReloadEnum.reload, (fp: string) => {
|
|
95
100
|
if (this.applicationStatus != AppStatusEnum.RUN) {
|
|
96
101
|
return;
|
|
97
102
|
}
|
|
98
103
|
|
|
99
|
-
this.addDelayHot(fp,
|
|
104
|
+
this.addDelayHot(fp, HotReloadEnum.reload);
|
|
100
105
|
});
|
|
101
106
|
|
|
102
|
-
this.on(
|
|
107
|
+
this.on(HotReloadEnum.sysReload, (fp: string) => {
|
|
103
108
|
if (fp.indexOf(this.basename) != -1) {
|
|
104
|
-
this.addDelayHot(fp,
|
|
109
|
+
this.addDelayHot(fp, HotReloadEnum.sysReload);
|
|
105
110
|
}
|
|
106
111
|
});
|
|
112
|
+
|
|
113
|
+
this.on(HotReloadEnum.configReload, (fp: string) => {
|
|
114
|
+
this.addDelayHot(fp, HotReloadEnum.configReload);
|
|
115
|
+
});
|
|
107
116
|
}
|
|
108
117
|
|
|
109
|
-
addDelayHot(fp: string, loadType:
|
|
118
|
+
addDelayHot(fp: string, loadType: HotReloadEnum) {
|
|
110
119
|
if (this.delayHotIds.has(fp)) {
|
|
111
120
|
return;
|
|
112
121
|
}
|
|
@@ -124,7 +133,7 @@ class FastCarApplication extends Events {
|
|
|
124
133
|
reloadFiles() {
|
|
125
134
|
this.delayHotIds.forEach(({ fp, loadType }) => {
|
|
126
135
|
switch (loadType) {
|
|
127
|
-
case
|
|
136
|
+
case HotReloadEnum.reload: {
|
|
128
137
|
let moduleClass = ClassLoader.loadModule(fp, true);
|
|
129
138
|
this.sysLogger.info("hot update---" + fp);
|
|
130
139
|
if (moduleClass != null) {
|
|
@@ -134,12 +143,26 @@ class FastCarApplication extends Events {
|
|
|
134
143
|
}
|
|
135
144
|
break;
|
|
136
145
|
}
|
|
137
|
-
case
|
|
146
|
+
case HotReloadEnum.sysReload: {
|
|
138
147
|
this.sysLogger.info("sysConfig hot update----" + fp);
|
|
139
148
|
this.loadSysConfig();
|
|
140
149
|
break;
|
|
141
150
|
}
|
|
151
|
+
case HotReloadEnum.configReload: {
|
|
152
|
+
let ids = this.hotConfigure.get(fp);
|
|
153
|
+
if (ids) {
|
|
154
|
+
ids.forEach((item) => {
|
|
155
|
+
let instance = this.getComponentByName(item);
|
|
156
|
+
if (instance) {
|
|
157
|
+
this.updateConfig(instance, fp);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
142
163
|
default: {
|
|
164
|
+
this.sysLogger.warn(`not found ${loadType} by ${fp}`);
|
|
165
|
+
break;
|
|
143
166
|
}
|
|
144
167
|
}
|
|
145
168
|
});
|
|
@@ -234,7 +257,7 @@ class FastCarApplication extends Events {
|
|
|
234
257
|
|
|
235
258
|
res = Reflect.get(this.sysConfig, key);
|
|
236
259
|
if (ValidationUtil.isNotNull(res)) {
|
|
237
|
-
res;
|
|
260
|
+
return res;
|
|
238
261
|
}
|
|
239
262
|
|
|
240
263
|
return Reflect.get(this, key);
|
|
@@ -364,117 +387,26 @@ class FastCarApplication extends Events {
|
|
|
364
387
|
let beforeKey = this.getInjectionUniqueKeyByFilePath(fp, iname);
|
|
365
388
|
|
|
366
389
|
if (beforeKey) {
|
|
367
|
-
let beforeInstance = this.
|
|
390
|
+
let beforeInstance = this.getBean(beforeKey);
|
|
368
391
|
if (!!beforeInstance) {
|
|
369
392
|
MixTool.assign(beforeInstance, classZ);
|
|
370
393
|
return;
|
|
371
394
|
}
|
|
372
395
|
}
|
|
373
396
|
|
|
374
|
-
|
|
375
|
-
let cp = Reflect.getMetadata(LifeCycleModule.LoadConfigure, Target);
|
|
376
|
-
if (cp) {
|
|
377
|
-
let rfp = path.join(this.getResourcePath(), cp);
|
|
378
|
-
let tmpConfig = FileUtil.getResource(rfp);
|
|
379
|
-
|
|
380
|
-
//进行实例化赋值
|
|
381
|
-
if (tmpConfig) {
|
|
382
|
-
//进行赋值不改变基础属性
|
|
383
|
-
if (TypeUtil.isFunction(classZ)) {
|
|
384
|
-
MixTool.copPropertyValue(classZ.prototype, tmpConfig);
|
|
385
|
-
} else {
|
|
386
|
-
MixTool.copPropertyValue(classZ, tmpConfig);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
let instance = TypeUtil.isFunction(classZ) ? new classZ() : classZ;
|
|
392
|
-
this.componentMap.set(instanceKey, instance);
|
|
397
|
+
//这边只放元数据
|
|
393
398
|
this.componentDeatils.set(instanceKey, {
|
|
394
399
|
id: instanceKey,
|
|
395
400
|
name: classZ?.name || FileUtil.getFileName(fp),
|
|
396
401
|
path: fp,
|
|
402
|
+
classZ,
|
|
397
403
|
});
|
|
398
404
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
if (aliasName) {
|
|
402
|
-
this.componentAliasMap.set(aliasName, instanceKey);
|
|
403
|
-
// this.componentMap.set(aliasName, instance);
|
|
404
|
-
// this.componentDeatils.set(aliasName, {
|
|
405
|
-
// id: aliasName,
|
|
406
|
-
// name: aliasName,
|
|
407
|
-
// path: fp,
|
|
408
|
-
// });
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
//判断是否需要热更加载
|
|
412
|
-
if (this.isHotter() || Reflect.getMetadata(FastCarMetaData.Hotter, instance)) {
|
|
413
|
-
let fpObj = this.watchFiles.get(fp);
|
|
414
|
-
let fpdesc = {
|
|
415
|
-
key: instanceKey,
|
|
416
|
-
name: iname,
|
|
417
|
-
};
|
|
418
|
-
if (!fpObj) {
|
|
419
|
-
fpObj = [fpdesc];
|
|
420
|
-
ClassLoader.watchServices(fp, this);
|
|
421
|
-
this.watchFiles.set(fp, fpObj);
|
|
422
|
-
} else {
|
|
423
|
-
fpObj.push(fpdesc);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
405
|
+
//加载Bean
|
|
406
|
+
this.getBean(instanceKey);
|
|
426
407
|
}
|
|
427
408
|
}
|
|
428
409
|
|
|
429
|
-
/***
|
|
430
|
-
* @version 1.0 装配模块
|
|
431
|
-
* @version 1.0 装配日志模块
|
|
432
|
-
* @version 1.1 移除装配日志模块 改为随用随取
|
|
433
|
-
* @deprecated 弃用系统自动装配
|
|
434
|
-
*/
|
|
435
|
-
// injectionModule(instance: any, instanceName: string | symbol): void {
|
|
436
|
-
// let relyname = FastCarMetaData.IocModule;
|
|
437
|
-
// let moduleList: Map<string, string> = Reflect.getMetadata(relyname, instance);
|
|
438
|
-
// let detailInfo = this.componentDeatils.get(instanceName);
|
|
439
|
-
|
|
440
|
-
// if (moduleList && moduleList.size > 0) {
|
|
441
|
-
// moduleList.forEach((name: string, propertyKey: string) => {
|
|
442
|
-
// let func = this.componentMap.get(name);
|
|
443
|
-
|
|
444
|
-
// //如果等于自身则进行注入
|
|
445
|
-
// if (name === FastCarApplication.name || name === FastCarMetaData.APP) {
|
|
446
|
-
// func = this;
|
|
447
|
-
// } else {
|
|
448
|
-
// if (!this.componentMap.has(name)) {
|
|
449
|
-
// //找不到依赖项
|
|
450
|
-
// let injectionError = new Error(`Unsatisfied dependency expressed through [${propertyKey}] in ${detailInfo?.path} `);
|
|
451
|
-
// this.sysLogger.error(injectionError.message);
|
|
452
|
-
// throw injectionError;
|
|
453
|
-
// }
|
|
454
|
-
// }
|
|
455
|
-
|
|
456
|
-
// Reflect.set(instance, propertyKey, func);
|
|
457
|
-
// });
|
|
458
|
-
// }
|
|
459
|
-
// }
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* @version 1.0 加载需要注入的类
|
|
463
|
-
* @deprecated
|
|
464
|
-
*/
|
|
465
|
-
// loadInjectionModule() {
|
|
466
|
-
// this.componentMap.forEach((instance, instanceName) => {
|
|
467
|
-
// //补充实例找不到时 不能被注解
|
|
468
|
-
// if (!instance) {
|
|
469
|
-
// let insatnceError = new Error(`instance not found by ${instanceName.toString()}`);
|
|
470
|
-
// this.sysLogger.error(insatnceError.message);
|
|
471
|
-
// throw insatnceError;
|
|
472
|
-
// }
|
|
473
|
-
|
|
474
|
-
// this.injectionModule(instance, instanceName);
|
|
475
|
-
// });
|
|
476
|
-
// }
|
|
477
|
-
|
|
478
410
|
/***
|
|
479
411
|
* @version 1.0 根据类型获取组件
|
|
480
412
|
*/
|
|
@@ -493,26 +425,156 @@ class FastCarApplication extends Events {
|
|
|
493
425
|
/***
|
|
494
426
|
* @version 1.0 获取全部的组件列表
|
|
495
427
|
*/
|
|
496
|
-
getComponentList():
|
|
428
|
+
getComponentList(): (Object | ClassConstructor<Object>)[] {
|
|
497
429
|
return [...this.componentMap.values()];
|
|
498
430
|
}
|
|
499
431
|
|
|
500
432
|
/***
|
|
501
433
|
* @version 1.0 根据名称组件
|
|
502
434
|
*/
|
|
503
|
-
getComponentByName(name: string | symbol):
|
|
435
|
+
getComponentByName(name: string | symbol): Object | null {
|
|
504
436
|
if (this.componentMap.has(name)) {
|
|
505
|
-
return this.
|
|
437
|
+
return this.getBean(name);
|
|
506
438
|
}
|
|
507
439
|
|
|
508
440
|
let key = this.componentAliasMap.get(name);
|
|
509
441
|
if (key) {
|
|
510
|
-
return this.
|
|
442
|
+
return this.getBean(key);
|
|
511
443
|
}
|
|
512
444
|
|
|
513
445
|
return null;
|
|
514
446
|
}
|
|
515
447
|
|
|
448
|
+
/**
|
|
449
|
+
* @version 1.0 组件改成按需加载的模式
|
|
450
|
+
*/
|
|
451
|
+
getBean(key: string | symbol): Object | null {
|
|
452
|
+
let instance = this.componentMap.get(key) || null;
|
|
453
|
+
if (!instance) {
|
|
454
|
+
//初始化
|
|
455
|
+
let item = this.componentDeatils.get(key);
|
|
456
|
+
if (!item) {
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
//判断是否有别名
|
|
461
|
+
let instanceKey = item.id;
|
|
462
|
+
let classZ = item.classZ as ClassConstructor<Object>;
|
|
463
|
+
let instance = TypeUtil.isFunction(classZ) ? new classZ() : classZ;
|
|
464
|
+
|
|
465
|
+
let hotter = this.isHotter();
|
|
466
|
+
if (!hotter) {
|
|
467
|
+
if (classZ?.prototype && Reflect.getMetadata(FastCarMetaData.Hotter, classZ.prototype)) {
|
|
468
|
+
hotter = true;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
//加载配置
|
|
473
|
+
let cp = Reflect.getMetadata(LifeCycleModule.LoadConfigure, classZ);
|
|
474
|
+
if (cp) {
|
|
475
|
+
let rfp = path.join(this.getResourcePath(), cp);
|
|
476
|
+
this.updateConfig(instance, rfp);
|
|
477
|
+
|
|
478
|
+
if (hotter) {
|
|
479
|
+
//监听资源文件
|
|
480
|
+
this.setHotConfigures(rfp, instanceKey);
|
|
481
|
+
ClassLoader.watchServices(rfp, this, HotReloadEnum.configReload);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
this.loadInjectionService(instance);
|
|
486
|
+
|
|
487
|
+
this.loadLoggerIOC(instance);
|
|
488
|
+
|
|
489
|
+
let aliasName = Reflect.getMetadata(FastCarMetaData.Alias, instance);
|
|
490
|
+
if (aliasName) {
|
|
491
|
+
this.componentAliasMap.set(aliasName, instanceKey);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
let fp = item.path;
|
|
495
|
+
let iname = classZ?.name || FileUtil.getFileName(fp);
|
|
496
|
+
|
|
497
|
+
//判断是否需要热更加载
|
|
498
|
+
if (hotter) {
|
|
499
|
+
let fpObj = this.watchFiles.get(fp);
|
|
500
|
+
let fpdesc = {
|
|
501
|
+
key: instanceKey,
|
|
502
|
+
name: iname,
|
|
503
|
+
};
|
|
504
|
+
if (!fpObj) {
|
|
505
|
+
fpObj = [fpdesc];
|
|
506
|
+
ClassLoader.watchServices(fp, this);
|
|
507
|
+
this.watchFiles.set(fp, fpObj);
|
|
508
|
+
} else {
|
|
509
|
+
fpObj.push(fpdesc);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
this.componentMap.set(key, instance);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return instance;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
public loadInjectionService(instance: Object) {
|
|
520
|
+
let injectionIds: Array<InjectionMeta> = Reflect.getMetadata(FastCarMetaData.InjectionSingleInstance, instance);
|
|
521
|
+
if (injectionIds && injectionIds.length > 0) {
|
|
522
|
+
injectionIds.forEach((item) => {
|
|
523
|
+
Reflect.defineProperty(instance, item.key, {
|
|
524
|
+
get: () => {
|
|
525
|
+
let key = ReflectUtil.getNameByPropertyKey(instance, item.alias || item.key);
|
|
526
|
+
if (!this.hasComponentByName(key)) {
|
|
527
|
+
//找不到依赖组件异常
|
|
528
|
+
let injectionError = new Error(`Unsatisfied dependency expressed through [${item.key}] `);
|
|
529
|
+
throw injectionError;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return this.getComponentByName(key);
|
|
533
|
+
},
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
public loadLoggerIOC(instance: Object) {
|
|
540
|
+
let logIds: Array<{
|
|
541
|
+
propertyKey: string;
|
|
542
|
+
name: string;
|
|
543
|
+
}> = Reflect.getMetadata(FastCarMetaData.InjectionLog, instance);
|
|
544
|
+
|
|
545
|
+
if (logIds && logIds.length > 0) {
|
|
546
|
+
logIds.forEach((item) => {
|
|
547
|
+
Reflect.defineProperty(instance, item.propertyKey, {
|
|
548
|
+
get: (): Logger => {
|
|
549
|
+
let appid = this.getSetting(CommonConstant.APPId) || ""; //进行差异化区分
|
|
550
|
+
return this.getLogger(appid ? `${appid}.${item.name}` : item.name);
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
private updateConfig(instance: Object, fp: string) {
|
|
558
|
+
let tmpConfig = FileUtil.getResource(fp);
|
|
559
|
+
|
|
560
|
+
//进行实例化赋值
|
|
561
|
+
if (tmpConfig) {
|
|
562
|
+
MixTool.copPropertyValue(instance, tmpConfig);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private setHotConfigures(fid: string, servicePath: string) {
|
|
567
|
+
let list = this.hotConfigure.get(fid);
|
|
568
|
+
if (!list) {
|
|
569
|
+
list = [];
|
|
570
|
+
this.hotConfigure.set(fid, list);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (!list.includes(servicePath)) {
|
|
574
|
+
list.push(servicePath);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
516
578
|
/***
|
|
517
579
|
* @version 1.0 判断是否拥有组件名称
|
|
518
580
|
*/
|
|
@@ -523,9 +585,11 @@ class FastCarApplication extends Events {
|
|
|
523
585
|
/***
|
|
524
586
|
* @version 1.0 根据原型获取实例
|
|
525
587
|
*/
|
|
526
|
-
getComponentByTarget(target: Object):
|
|
588
|
+
getComponentByTarget<T>(target: Object): T | null {
|
|
527
589
|
let key = this.getInjectionUniqueKey(target);
|
|
528
|
-
|
|
590
|
+
let bean = this.getBean(key);
|
|
591
|
+
|
|
592
|
+
return bean != null ? (bean as T) : null;
|
|
529
593
|
}
|
|
530
594
|
|
|
531
595
|
/**
|
|
@@ -574,8 +638,6 @@ class FastCarApplication extends Events {
|
|
|
574
638
|
}
|
|
575
639
|
|
|
576
640
|
this.loggerFactory = new WinstonLogger(defaultConfig);
|
|
577
|
-
//添加系统日志
|
|
578
|
-
// this.sysLogger = this.loggerFactory.addLogger(CommonConstant.SYSLOGGER);
|
|
579
641
|
}
|
|
580
642
|
|
|
581
643
|
/***
|
|
@@ -587,6 +649,7 @@ class FastCarApplication extends Events {
|
|
|
587
649
|
this.baseFileName = (Reflect.get(this, CommonConstant.BaseFileName) || require.main?.filename || module.filename) as string;
|
|
588
650
|
|
|
589
651
|
this.beforeStartServer();
|
|
652
|
+
|
|
590
653
|
this.startServer();
|
|
591
654
|
this.addExitEvent();
|
|
592
655
|
this.addExecptionEvent();
|
|
@@ -678,19 +741,17 @@ class FastCarApplication extends Events {
|
|
|
678
741
|
|
|
679
742
|
//监听系统配置
|
|
680
743
|
if (this.isHotterSysConfig()) {
|
|
681
|
-
ClassLoader.watchServices(this.getResourcePath(), this,
|
|
744
|
+
ClassLoader.watchServices(this.getResourcePath(), this, HotReloadEnum.sysReload);
|
|
682
745
|
}
|
|
683
746
|
|
|
684
747
|
//开启日志
|
|
685
748
|
this.startLog();
|
|
686
749
|
|
|
750
|
+
this.loadLoggerIOC(this);
|
|
751
|
+
|
|
687
752
|
this.sysLogger.info("Start scanning component");
|
|
688
753
|
this.loadClass();
|
|
689
754
|
this.sysLogger.info("Complete component scan");
|
|
690
|
-
|
|
691
|
-
// this.sysLogger.info("Start component injection");
|
|
692
|
-
// this.loadInjectionModule();
|
|
693
|
-
// this.sysLogger.info("Complete component injection");
|
|
694
755
|
}
|
|
695
756
|
|
|
696
757
|
/***
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import FastCarApplication from "../FastCarApplication";
|
|
2
|
+
import { ClassConstructor } from "../type/ClassConstructor";
|
|
2
3
|
import ClassUtils from "../utils/ClassUtils";
|
|
3
4
|
import TypeUtil from "../utils/TypeUtil";
|
|
4
5
|
|
|
5
6
|
//基础服务的应用
|
|
6
7
|
export default function Application(target: any) {
|
|
7
8
|
return new Proxy(target, {
|
|
8
|
-
construct: (target:
|
|
9
|
+
construct: (target: ClassConstructor<Object>, args: any) => {
|
|
9
10
|
let app = new FastCarApplication();
|
|
10
11
|
let appProxy = new target(...args);
|
|
11
12
|
Reflect.set(appProxy, "app", app);
|
|
@@ -39,6 +40,7 @@ export default function Application(target: any) {
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
app.loadLoggerIOC(appProxy);
|
|
42
44
|
app.init();
|
|
43
45
|
return appProxy;
|
|
44
46
|
},
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FastCarMetaData } from "../../constant/FastCarMetaData";
|
|
2
|
+
import { InjectionMeta, InjectionType } from "../../type/ComponentDesc";
|
|
3
|
+
|
|
4
|
+
export function AddInjectionService({ target, propertyKey, kind, alias }: { target: Object; propertyKey: string; kind: InjectionType; alias?: string }) {
|
|
5
|
+
let services: Array<InjectionMeta> = Reflect.getMetadata(FastCarMetaData.InjectionSingleInstance, target);
|
|
6
|
+
|
|
7
|
+
if (!services) {
|
|
8
|
+
services = [];
|
|
9
|
+
Reflect.defineMetadata(FastCarMetaData.InjectionSingleInstance, services, target);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
services.push({
|
|
13
|
+
key: propertyKey,
|
|
14
|
+
kind: kind,
|
|
15
|
+
alias,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -5,7 +5,7 @@ import { FastCarMetaData } from "../../constant/FastCarMetaData";
|
|
|
5
5
|
* @version 1.0 依赖模块注入
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
export default function AddRequireModule(target:
|
|
8
|
+
export default function AddRequireModule(target: Object, m: string, alias: string) {
|
|
9
9
|
let relyname = FastCarMetaData.IocModule;
|
|
10
10
|
if (Reflect.hasMetadata(relyname, target)) {
|
|
11
11
|
let iocMap: Map<string, string> = Reflect.getMetadata(relyname, target);
|
|
@@ -1,24 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { InjectionType } from "../../type/ComponentDesc";
|
|
2
|
+
import { AddInjectionService } from "./AddInjectionService";
|
|
3
3
|
|
|
4
4
|
/***
|
|
5
5
|
* @version 1.0 根据别名注入依赖
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
export default function AliasInjection(alias: string) {
|
|
9
|
-
return function (target:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
//找不到依赖组件异常
|
|
16
|
-
let injectionError = new Error(`Unsatisfied dependency expressed through [${propertyKey}] `);
|
|
17
|
-
throw injectionError;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return app.getComponentByName(alias);
|
|
21
|
-
},
|
|
9
|
+
return function (target: Object, propertyKey: string) {
|
|
10
|
+
AddInjectionService({
|
|
11
|
+
target,
|
|
12
|
+
propertyKey,
|
|
13
|
+
alias,
|
|
14
|
+
kind: InjectionType.ALIAS,
|
|
22
15
|
});
|
|
23
16
|
};
|
|
24
17
|
}
|
|
@@ -5,7 +5,7 @@ import CallDependency from "./CallDependency";
|
|
|
5
5
|
* @version 1.1 更改为和call类型一致
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
export default function Autowired(target:
|
|
8
|
+
export default function Autowired(target: Object, propertyKey: string) {
|
|
9
9
|
// //反向找设计类型
|
|
10
10
|
CallDependency(target, propertyKey);
|
|
11
11
|
}
|
|
@@ -1,24 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import ReflectUtil from "../../utils/ReflectUtil";
|
|
1
|
+
import { InjectionType } from "../../type/ComponentDesc";
|
|
2
|
+
import { AddInjectionService } from "./AddInjectionService";
|
|
4
3
|
|
|
5
4
|
/***
|
|
6
5
|
* @version 1.0 在使用该函数时进行调用 声明的类可以不是组件
|
|
7
6
|
*
|
|
8
7
|
*/
|
|
9
|
-
export default function CallDependency(target:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (!app.hasComponentByName(key)) {
|
|
16
|
-
//找不到依赖组件异常
|
|
17
|
-
let injectionError = new Error(`Unsatisfied dependency expressed through [${propertyKey}] `);
|
|
18
|
-
throw injectionError;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return app.getComponentByName(key);
|
|
22
|
-
},
|
|
8
|
+
export default function CallDependency(target: Object, propertyKey: string) {
|
|
9
|
+
AddInjectionService({
|
|
10
|
+
target,
|
|
11
|
+
propertyKey,
|
|
12
|
+
kind: InjectionType.PROPERTYKEY,
|
|
23
13
|
});
|
|
24
14
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//延迟注入作用用于类上,用于没有初始时的依赖注入
|
|
2
|
+
import { CommonConstant } from "../../constant/CommonConstant";
|
|
3
|
+
import FastCarApplication from "../../FastCarApplication";
|
|
4
|
+
import { ClassConstructor } from "../../type/ClassConstructor";
|
|
5
|
+
|
|
6
|
+
export default function DemandInjection(Target: ClassConstructor<any>) {
|
|
7
|
+
return new Proxy(Target, {
|
|
8
|
+
construct: (Target: ClassConstructor<any>, args: any) => {
|
|
9
|
+
let c = new Target(...args);
|
|
10
|
+
let app: FastCarApplication = Reflect.get(global, CommonConstant.FastcarApp);
|
|
11
|
+
|
|
12
|
+
app?.loadInjectionService(c);
|
|
13
|
+
return c;
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import ApplicationInterface from "../../interface/ApplicationInterface";
|
|
3
|
-
import Logger from "../../interface/Logger";
|
|
1
|
+
import { FastCarMetaData } from "../../constant/FastCarMetaData";
|
|
4
2
|
|
|
5
3
|
//日志实例
|
|
6
4
|
export default function Log(category?: string) {
|
|
7
5
|
return function (target: any, propertyKey: string) {
|
|
8
6
|
let m = category || propertyKey;
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
let services: Array<{
|
|
9
|
+
propertyKey: string;
|
|
10
|
+
name: string;
|
|
11
|
+
}> = Reflect.getMetadata(FastCarMetaData.InjectionLog, target);
|
|
12
|
+
if (!services) {
|
|
13
|
+
services = [];
|
|
14
|
+
Reflect.defineMetadata(FastCarMetaData.InjectionLog, services, target);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
services.push({
|
|
18
|
+
propertyKey,
|
|
19
|
+
name: m,
|
|
16
20
|
});
|
|
17
21
|
};
|
|
18
22
|
}
|
package/src/annotation.ts
CHANGED
|
@@ -50,6 +50,7 @@ import ResourcePath from "./annotation/env/ResourcePath";
|
|
|
50
50
|
import BaseName from "./annotation/env/BaseName";
|
|
51
51
|
import CustomType from "./annotation/valid/CustomType";
|
|
52
52
|
import ComponentScanMust from "./annotation/scan/ComponentScanMust";
|
|
53
|
+
import DemandInjection from "./annotation/bind/DemandInjection";
|
|
53
54
|
|
|
54
55
|
//注解暴露出去
|
|
55
56
|
export {
|
|
@@ -73,6 +74,7 @@ export {
|
|
|
73
74
|
Application,
|
|
74
75
|
Autowired,
|
|
75
76
|
CallDependency,
|
|
77
|
+
DemandInjection,
|
|
76
78
|
AliasInjection,
|
|
77
79
|
ExceptionMonitor,
|
|
78
80
|
Deprecate,
|
|
@@ -24,4 +24,6 @@ export enum FastCarMetaData {
|
|
|
24
24
|
HotterSysConfig = "hotterSysConfig", //支持热更系统配置
|
|
25
25
|
CustomType = "custom:type", //自定义类型
|
|
26
26
|
ComponentScanMust = "ComponentScanMust", //扫描路径
|
|
27
|
+
InjectionSingleInstance = "InjectionSingleInstance", //单个实例注入的服务
|
|
28
|
+
InjectionLog = "InjectionLog", //反射的日志方法
|
|
27
29
|
}
|
|
@@ -79,7 +79,7 @@ export default class WinstonLogger {
|
|
|
79
79
|
text += this.colorizeData(content.message);
|
|
80
80
|
|
|
81
81
|
if (Reflect.has(info, SPLAT)) {
|
|
82
|
-
let splatMsg: any[] = Reflect.get(info, SPLAT);
|
|
82
|
+
let splatMsg: any[] = Reflect.get(info, SPLAT) as any;
|
|
83
83
|
splatMsg.forEach((item) => {
|
|
84
84
|
text += " " + this.colorizeData(item);
|
|
85
85
|
});
|