2020-06-17 10:34:01 +00:00
|
|
|
/// <reference types="@ccms/nashorn" />
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
import "reflect-metadata"
|
|
|
|
import { initContainer, getContainer } from './decorators'
|
|
|
|
import { interfaces, Container } from 'inversify'
|
|
|
|
import { fluentProvide } from 'inversify-binding-decorators'
|
|
|
|
import { ioc } from "./constants"
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
/**
|
|
|
|
* 注册一个命名对象
|
|
|
|
* @param identifier 标识符
|
|
|
|
* @param name 名称
|
|
|
|
*/
|
|
|
|
export const provideNamed = (identifier: interfaces.ServiceIdentifier<any>, name: string) => {
|
|
|
|
return fluentProvide(identifier).whenTargetNamed(name).done()
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
/**
|
|
|
|
* 注册一个单例对象
|
|
|
|
* @param identifier 标识符
|
|
|
|
*/
|
|
|
|
export const provideSingleton = (identifier: interfaces.ServiceIdentifier<any>) => {
|
|
|
|
return fluentProvide(identifier).inSingletonScope().done()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 注册一个单例对象
|
|
|
|
* @param identifier 标识符
|
|
|
|
*/
|
|
|
|
export const provideSingletonNamed = (identifier: interfaces.ServiceIdentifier<any>, name: string) => {
|
|
|
|
return fluentProvide(identifier).inSingletonScope().whenTargetNamed(name).done()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获得一个 java.lang.Class
|
|
|
|
* @param className Java全类名
|
|
|
|
*/
|
|
|
|
export const JavaClass = (className: string) => {
|
2020-09-17 09:44:48 +00:00
|
|
|
return function (target: object, propertyKey: string, index?: number) {
|
2020-06-17 10:34:01 +00:00
|
|
|
try { target[propertyKey] = Java.type(className).class; return } catch (error) { }
|
|
|
|
try { target[propertyKey] = base.getClass(className); return } catch (error) { }
|
2020-09-17 09:44:48 +00:00
|
|
|
console.warn('JavaClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
|
2020-06-17 10:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获得一个JS的Java类
|
|
|
|
* @param className Java 全类名
|
|
|
|
*/
|
|
|
|
export const JSClass = (className: string) => {
|
2020-09-17 09:44:48 +00:00
|
|
|
return function (target: object, propertyKey: string, index?: number) {
|
2020-06-17 10:34:01 +00:00
|
|
|
try { target[propertyKey] = Java.type(className); return } catch (error) { }
|
|
|
|
try { target[propertyKey] = base.getClass(className).static; return } catch (error) { }
|
2020-09-17 09:44:48 +00:00
|
|
|
console.warn('JSClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
|
2020-06-17 10:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 自动注入实例由平台实现
|
|
|
|
* @param className 类名
|
|
|
|
*/
|
|
|
|
export const Autowired = (className?: string | any) => {
|
2020-09-22 09:33:59 +00:00
|
|
|
return function (target: any, propertyKey: string, index?: number) {
|
|
|
|
let container = getContainer()
|
|
|
|
let type = Reflect.getMetadata('design:type', target, propertyKey)
|
|
|
|
if (type && type !== Object) {
|
|
|
|
try {
|
|
|
|
return target[propertyKey] = container.getNamed(type, className || propertyKey)
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
return target[propertyKey] = container.get(type)
|
|
|
|
} catch (error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (container.isBound(ioc.Autowired)) {
|
|
|
|
target[propertyKey] = container.getNamed(ioc.Autowired, className || propertyKey)
|
|
|
|
}
|
2020-06-17 10:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 自动注入资源由平台实现
|
|
|
|
* @param className 类名
|
|
|
|
*/
|
|
|
|
export const Resource = (resourceName?: string | any) => {
|
2020-09-22 09:33:59 +00:00
|
|
|
return function (target: any, propertyKey: string, index?: number) {
|
2020-06-17 10:34:01 +00:00
|
|
|
target[propertyKey] = getContainer().getNamed(ioc.Resource, resourceName || propertyKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const reduceMetadata = (ctx: interfaces.Context): any => {
|
|
|
|
return ctx.currentRequest.target.metadata.reduce((result, entry, index) => {
|
|
|
|
result[entry.key] = entry.value
|
|
|
|
return result
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
2020-09-22 09:33:59 +00:00
|
|
|
function initAutowired(container: Container) {
|
|
|
|
container.bind(ioc.Autowired).toDynamicValue((ctx) => {
|
|
|
|
var metadata: any = reduceMetadata(ctx)
|
|
|
|
let key = Object.toString.call(metadata.named)
|
|
|
|
if (key === "[object Function]" || key === "[object Symbol]") { return container.get(metadata.named) }
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
export const DefaultContainer = new Container()
|
|
|
|
initContainer(DefaultContainer)
|
2020-09-22 09:33:59 +00:00
|
|
|
initAutowired(DefaultContainer)
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
export * from 'inversify'
|
2020-06-17 10:34:01 +00:00
|
|
|
export * from './constants'
|
2019-09-07 04:23:15 +00:00
|
|
|
export * from './decorators'
|
|
|
|
export * from 'inversify-binding-decorators'
|