@fastcar/core 0.2.64 → 0.3.1
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 +3 -3
- package/src/FastCarApplication.ts +185 -122
- package/src/annotation/Application.ts +5 -2
- 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 +15 -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/target/FastCarApplication.js +765 -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/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/multi/app.ts +2 -2
- 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 { ClassConstructor } from "./type/ClassConstructor";
|
|
29
|
+
import ReflectUtil from "./utils/ReflectUtil";
|
|
28
30
|
import Log from "./annotation/stereotype/Log";
|
|
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,9 @@ 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();
|
|
67
|
+
|
|
68
|
+
this.sysLogger = console;
|
|
63
69
|
|
|
64
70
|
this.loadSelf();
|
|
65
71
|
this.addHot();
|
|
@@ -81,6 +87,7 @@ class FastCarApplication extends Events {
|
|
|
81
87
|
id: key,
|
|
82
88
|
name: "FastCarApplication",
|
|
83
89
|
path: __filename,
|
|
90
|
+
classZ: this,
|
|
84
91
|
});
|
|
85
92
|
//暴露一个全局的app 以便调用
|
|
86
93
|
Reflect.set(global, CommonConstant.FastcarApp, this);
|
|
@@ -91,22 +98,26 @@ class FastCarApplication extends Events {
|
|
|
91
98
|
* @version 1.1 热更新配置文件
|
|
92
99
|
*/
|
|
93
100
|
addHot() {
|
|
94
|
-
this.on(
|
|
101
|
+
this.on(HotReloadEnum.reload, (fp: string) => {
|
|
95
102
|
if (this.applicationStatus != AppStatusEnum.RUN) {
|
|
96
103
|
return;
|
|
97
104
|
}
|
|
98
105
|
|
|
99
|
-
this.addDelayHot(fp,
|
|
106
|
+
this.addDelayHot(fp, HotReloadEnum.reload);
|
|
100
107
|
});
|
|
101
108
|
|
|
102
|
-
this.on(
|
|
109
|
+
this.on(HotReloadEnum.sysReload, (fp: string) => {
|
|
103
110
|
if (fp.indexOf(this.basename) != -1) {
|
|
104
|
-
this.addDelayHot(fp,
|
|
111
|
+
this.addDelayHot(fp, HotReloadEnum.sysReload);
|
|
105
112
|
}
|
|
106
113
|
});
|
|
114
|
+
|
|
115
|
+
this.on(HotReloadEnum.configReload, (fp: string) => {
|
|
116
|
+
this.addDelayHot(fp, HotReloadEnum.configReload);
|
|
117
|
+
});
|
|
107
118
|
}
|
|
108
119
|
|
|
109
|
-
addDelayHot(fp: string, loadType:
|
|
120
|
+
addDelayHot(fp: string, loadType: HotReloadEnum) {
|
|
110
121
|
if (this.delayHotIds.has(fp)) {
|
|
111
122
|
return;
|
|
112
123
|
}
|
|
@@ -124,7 +135,7 @@ class FastCarApplication extends Events {
|
|
|
124
135
|
reloadFiles() {
|
|
125
136
|
this.delayHotIds.forEach(({ fp, loadType }) => {
|
|
126
137
|
switch (loadType) {
|
|
127
|
-
case
|
|
138
|
+
case HotReloadEnum.reload: {
|
|
128
139
|
let moduleClass = ClassLoader.loadModule(fp, true);
|
|
129
140
|
this.sysLogger.info("hot update---" + fp);
|
|
130
141
|
if (moduleClass != null) {
|
|
@@ -134,12 +145,26 @@ class FastCarApplication extends Events {
|
|
|
134
145
|
}
|
|
135
146
|
break;
|
|
136
147
|
}
|
|
137
|
-
case
|
|
148
|
+
case HotReloadEnum.sysReload: {
|
|
138
149
|
this.sysLogger.info("sysConfig hot update----" + fp);
|
|
139
150
|
this.loadSysConfig();
|
|
140
151
|
break;
|
|
141
152
|
}
|
|
153
|
+
case HotReloadEnum.configReload: {
|
|
154
|
+
let ids = this.hotConfigure.get(fp);
|
|
155
|
+
if (ids) {
|
|
156
|
+
ids.forEach((item) => {
|
|
157
|
+
let instance = this.getComponentByName(item);
|
|
158
|
+
if (instance) {
|
|
159
|
+
this.updateConfig(instance, fp);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
142
165
|
default: {
|
|
166
|
+
this.sysLogger.warn(`not found ${loadType} by ${fp}`);
|
|
167
|
+
break;
|
|
143
168
|
}
|
|
144
169
|
}
|
|
145
170
|
});
|
|
@@ -234,7 +259,7 @@ class FastCarApplication extends Events {
|
|
|
234
259
|
|
|
235
260
|
res = Reflect.get(this.sysConfig, key);
|
|
236
261
|
if (ValidationUtil.isNotNull(res)) {
|
|
237
|
-
res;
|
|
262
|
+
return res;
|
|
238
263
|
}
|
|
239
264
|
|
|
240
265
|
return Reflect.get(this, key);
|
|
@@ -364,117 +389,26 @@ class FastCarApplication extends Events {
|
|
|
364
389
|
let beforeKey = this.getInjectionUniqueKeyByFilePath(fp, iname);
|
|
365
390
|
|
|
366
391
|
if (beforeKey) {
|
|
367
|
-
let beforeInstance = this.
|
|
392
|
+
let beforeInstance = this.getBean(beforeKey);
|
|
368
393
|
if (!!beforeInstance) {
|
|
369
394
|
MixTool.assign(beforeInstance, classZ);
|
|
370
395
|
return;
|
|
371
396
|
}
|
|
372
397
|
}
|
|
373
398
|
|
|
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);
|
|
399
|
+
//这边只放元数据
|
|
393
400
|
this.componentDeatils.set(instanceKey, {
|
|
394
401
|
id: instanceKey,
|
|
395
402
|
name: classZ?.name || FileUtil.getFileName(fp),
|
|
396
403
|
path: fp,
|
|
404
|
+
classZ,
|
|
397
405
|
});
|
|
398
406
|
|
|
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
|
-
}
|
|
407
|
+
//加载Bean
|
|
408
|
+
this.getBean(instanceKey);
|
|
426
409
|
}
|
|
427
410
|
}
|
|
428
411
|
|
|
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
412
|
/***
|
|
479
413
|
* @version 1.0 根据类型获取组件
|
|
480
414
|
*/
|
|
@@ -493,26 +427,156 @@ class FastCarApplication extends Events {
|
|
|
493
427
|
/***
|
|
494
428
|
* @version 1.0 获取全部的组件列表
|
|
495
429
|
*/
|
|
496
|
-
getComponentList():
|
|
430
|
+
getComponentList(): (Object | ClassConstructor<Object>)[] {
|
|
497
431
|
return [...this.componentMap.values()];
|
|
498
432
|
}
|
|
499
433
|
|
|
500
434
|
/***
|
|
501
435
|
* @version 1.0 根据名称组件
|
|
502
436
|
*/
|
|
503
|
-
getComponentByName(name: string | symbol):
|
|
437
|
+
getComponentByName(name: string | symbol): Object | null {
|
|
504
438
|
if (this.componentMap.has(name)) {
|
|
505
|
-
return this.
|
|
439
|
+
return this.getBean(name);
|
|
506
440
|
}
|
|
507
441
|
|
|
508
442
|
let key = this.componentAliasMap.get(name);
|
|
509
443
|
if (key) {
|
|
510
|
-
return this.
|
|
444
|
+
return this.getBean(key);
|
|
511
445
|
}
|
|
512
446
|
|
|
513
447
|
return null;
|
|
514
448
|
}
|
|
515
449
|
|
|
450
|
+
/**
|
|
451
|
+
* @version 1.0 组件改成按需加载的模式
|
|
452
|
+
*/
|
|
453
|
+
getBean(key: string | symbol): Object | null {
|
|
454
|
+
let instance = this.componentMap.get(key) || null;
|
|
455
|
+
if (!instance) {
|
|
456
|
+
//初始化
|
|
457
|
+
let item = this.componentDeatils.get(key);
|
|
458
|
+
if (!item) {
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
//判断是否有别名
|
|
463
|
+
let instanceKey = item.id;
|
|
464
|
+
let classZ = item.classZ as ClassConstructor<Object>;
|
|
465
|
+
let instance = TypeUtil.isFunction(classZ) ? new classZ() : classZ;
|
|
466
|
+
|
|
467
|
+
let hotter = this.isHotter();
|
|
468
|
+
if (!hotter) {
|
|
469
|
+
if (classZ?.prototype && Reflect.getMetadata(FastCarMetaData.Hotter, classZ.prototype)) {
|
|
470
|
+
hotter = true;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
//加载配置
|
|
475
|
+
let cp = Reflect.getMetadata(LifeCycleModule.LoadConfigure, classZ);
|
|
476
|
+
if (cp) {
|
|
477
|
+
let rfp = path.join(this.getResourcePath(), cp);
|
|
478
|
+
this.updateConfig(instance, rfp);
|
|
479
|
+
|
|
480
|
+
if (hotter) {
|
|
481
|
+
//监听资源文件
|
|
482
|
+
this.setHotConfigures(rfp, instanceKey);
|
|
483
|
+
ClassLoader.watchServices(rfp, this, HotReloadEnum.configReload);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
this.loadInjectionService(instance);
|
|
488
|
+
|
|
489
|
+
this.loadLoggerIOC(instance);
|
|
490
|
+
|
|
491
|
+
let aliasName = Reflect.getMetadata(FastCarMetaData.Alias, instance);
|
|
492
|
+
if (aliasName) {
|
|
493
|
+
this.componentAliasMap.set(aliasName, instanceKey);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
let fp = item.path;
|
|
497
|
+
let iname = classZ?.name || FileUtil.getFileName(fp);
|
|
498
|
+
|
|
499
|
+
//判断是否需要热更加载
|
|
500
|
+
if (hotter) {
|
|
501
|
+
let fpObj = this.watchFiles.get(fp);
|
|
502
|
+
let fpdesc = {
|
|
503
|
+
key: instanceKey,
|
|
504
|
+
name: iname,
|
|
505
|
+
};
|
|
506
|
+
if (!fpObj) {
|
|
507
|
+
fpObj = [fpdesc];
|
|
508
|
+
ClassLoader.watchServices(fp, this);
|
|
509
|
+
this.watchFiles.set(fp, fpObj);
|
|
510
|
+
} else {
|
|
511
|
+
fpObj.push(fpdesc);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
this.componentMap.set(key, instance);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return instance;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
public loadInjectionService(instance: Object) {
|
|
522
|
+
let injectionIds: Array<InjectionMeta> = Reflect.getMetadata(FastCarMetaData.InjectionSingleInstance, instance);
|
|
523
|
+
if (injectionIds && injectionIds.length > 0) {
|
|
524
|
+
injectionIds.forEach((item) => {
|
|
525
|
+
Reflect.defineProperty(instance, item.key, {
|
|
526
|
+
get: () => {
|
|
527
|
+
let key = ReflectUtil.getNameByPropertyKey(instance, item.alias || item.key);
|
|
528
|
+
if (!this.hasComponentByName(key)) {
|
|
529
|
+
//找不到依赖组件异常
|
|
530
|
+
let injectionError = new Error(`Unsatisfied dependency expressed through [${item.key}] `);
|
|
531
|
+
throw injectionError;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return this.getComponentByName(key);
|
|
535
|
+
},
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
public loadLoggerIOC(instance: Object) {
|
|
542
|
+
let logIds: Array<{
|
|
543
|
+
propertyKey: string;
|
|
544
|
+
name: string;
|
|
545
|
+
}> = Reflect.getMetadata(FastCarMetaData.InjectionLog, instance);
|
|
546
|
+
|
|
547
|
+
if (logIds && logIds.length > 0) {
|
|
548
|
+
logIds.forEach((item) => {
|
|
549
|
+
Reflect.defineProperty(instance, item.propertyKey, {
|
|
550
|
+
get: (): Logger => {
|
|
551
|
+
let appid = this.getSetting(CommonConstant.APPId) || ""; //进行差异化区分
|
|
552
|
+
return this.getLogger(appid ? `${appid}.${item.name}` : item.name);
|
|
553
|
+
},
|
|
554
|
+
});
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
private updateConfig(instance: Object, fp: string) {
|
|
560
|
+
let tmpConfig = FileUtil.getResource(fp);
|
|
561
|
+
|
|
562
|
+
//进行实例化赋值
|
|
563
|
+
if (tmpConfig) {
|
|
564
|
+
MixTool.copPropertyValue(instance, tmpConfig);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
private setHotConfigures(fid: string, servicePath: string) {
|
|
569
|
+
let list = this.hotConfigure.get(fid);
|
|
570
|
+
if (!list) {
|
|
571
|
+
list = [];
|
|
572
|
+
this.hotConfigure.set(fid, list);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (!list.includes(servicePath)) {
|
|
576
|
+
list.push(servicePath);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
516
580
|
/***
|
|
517
581
|
* @version 1.0 判断是否拥有组件名称
|
|
518
582
|
*/
|
|
@@ -523,9 +587,11 @@ class FastCarApplication extends Events {
|
|
|
523
587
|
/***
|
|
524
588
|
* @version 1.0 根据原型获取实例
|
|
525
589
|
*/
|
|
526
|
-
getComponentByTarget(target: Object):
|
|
590
|
+
getComponentByTarget<T>(target: Object): T | null {
|
|
527
591
|
let key = this.getInjectionUniqueKey(target);
|
|
528
|
-
|
|
592
|
+
let bean = this.getBean(key);
|
|
593
|
+
|
|
594
|
+
return bean != null ? (bean as T) : null;
|
|
529
595
|
}
|
|
530
596
|
|
|
531
597
|
/**
|
|
@@ -574,8 +640,6 @@ class FastCarApplication extends Events {
|
|
|
574
640
|
}
|
|
575
641
|
|
|
576
642
|
this.loggerFactory = new WinstonLogger(defaultConfig);
|
|
577
|
-
//添加系统日志
|
|
578
|
-
// this.sysLogger = this.loggerFactory.addLogger(CommonConstant.SYSLOGGER);
|
|
579
643
|
}
|
|
580
644
|
|
|
581
645
|
/***
|
|
@@ -587,6 +651,7 @@ class FastCarApplication extends Events {
|
|
|
587
651
|
this.baseFileName = (Reflect.get(this, CommonConstant.BaseFileName) || require.main?.filename || module.filename) as string;
|
|
588
652
|
|
|
589
653
|
this.beforeStartServer();
|
|
654
|
+
|
|
590
655
|
this.startServer();
|
|
591
656
|
this.addExitEvent();
|
|
592
657
|
this.addExecptionEvent();
|
|
@@ -678,19 +743,17 @@ class FastCarApplication extends Events {
|
|
|
678
743
|
|
|
679
744
|
//监听系统配置
|
|
680
745
|
if (this.isHotterSysConfig()) {
|
|
681
|
-
ClassLoader.watchServices(this.getResourcePath(), this,
|
|
746
|
+
ClassLoader.watchServices(this.getResourcePath(), this, HotReloadEnum.sysReload);
|
|
682
747
|
}
|
|
683
748
|
|
|
684
749
|
//开启日志
|
|
685
750
|
this.startLog();
|
|
686
751
|
|
|
752
|
+
this.loadLoggerIOC(this);
|
|
753
|
+
|
|
687
754
|
this.sysLogger.info("Start scanning component");
|
|
688
755
|
this.loadClass();
|
|
689
756
|
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
757
|
}
|
|
695
758
|
|
|
696
759
|
/***
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ClassConstructor } from "../type/ClassConstructor";
|
|
2
2
|
import ClassUtils from "../utils/ClassUtils";
|
|
3
3
|
import TypeUtil from "../utils/TypeUtil";
|
|
4
4
|
|
|
5
5
|
//基础服务的应用
|
|
6
6
|
export default function Application(target: any) {
|
|
7
7
|
return new Proxy(target, {
|
|
8
|
-
construct: (target:
|
|
8
|
+
construct: (target: ClassConstructor<Object>, args: any) => {
|
|
9
|
+
const FastCarApplication = require("../FastCarApplication").default;
|
|
10
|
+
|
|
9
11
|
let app = new FastCarApplication();
|
|
10
12
|
let appProxy = new target(...args);
|
|
11
13
|
Reflect.set(appProxy, "app", app);
|
|
@@ -39,6 +41,7 @@ export default function Application(target: any) {
|
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
app.loadLoggerIOC(appProxy);
|
|
42
45
|
app.init();
|
|
43
46
|
return appProxy;
|
|
44
47
|
},
|
|
@@ -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,15 @@
|
|
|
1
|
+
//延迟注入作用用于类上,用于没有初始时的依赖注入
|
|
2
|
+
import { CommonConstant } from "../../constant/CommonConstant";
|
|
3
|
+
import { ClassConstructor } from "../../type/ClassConstructor";
|
|
4
|
+
|
|
5
|
+
export default function DemandInjection(Target: ClassConstructor<any>) {
|
|
6
|
+
return new Proxy(Target, {
|
|
7
|
+
construct: (Target: ClassConstructor<any>, args: any) => {
|
|
8
|
+
let c = new Target(...args);
|
|
9
|
+
let app: any = Reflect.get(global, CommonConstant.FastcarApp);
|
|
10
|
+
|
|
11
|
+
app?.loadInjectionService(c);
|
|
12
|
+
return c;
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -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
|
}
|