用Typescript开发js基础库

基于基础库的开发经验搭建一套类似Spring的基础框架库。

1、解决组件与组件之间的通讯;

2、解决Component的注册、生命周期、初始化;

3、解决Bean之间的通讯,利用依赖注入机制可以直接获取上下文中的Bean;

4、解决模块与模块之间的隔离;

@Bean  在上下文中获取单实例

  • 代码实现
export function Bean(beanName: string): Function {
    // console.log('Bean >> ', beanName);
    return (classConstructor: any) => {
        const props = getOrCreateProps(classConstructor);
        props.beanName = beanName;
    };
}
  • 使用场景
/**
 * 日志工厂类
 */
@Bean('loggerFactory')
export class LoggerFactory {

}

 

@Autowired  把Bean注入到对应的属性上

  • 代码实现
export function Autowired(name?: string): Function {
    // console.log('Autowired >> ', name);
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        autowiredFunc(target, name, false, target, propertyKey, null);
    };
}

function autowiredFunc(target: any, name: string, optional: boolean, classPrototype: any, methodOrAttributeName: string, index: number) {
    if (name === null) {
        console.error("Context: Autowired name should not be null");
        return;
    }
    if (typeof index === "number") {
        console.error("Context: Autowired should be on an attribute");
        return;
    }

    // it's an attribute on the class
    const props = getOrCreateProps(target.constructor);
    const classAttributes = 'agClassAttributes';
    if (!props[classAttributes]) {
        props[classAttributes] = [];
    }
    props[classAttributes].push({
        attributeName: methodOrAttributeName,
        beanName: name,
        optional: optional
    });
}

 

  • 使用场景
@Bean('model')
export class BaseModel implements IModel {

    @Autowired('loggerFactory') private loggerFactory: LoggerFactory;

}

 @Optional

  • 代码实现
  • export function Optional(name?: string): Function {
        // console.log('Optional >> ', name);
        return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
            autowiredFunc(target, name, true, target, propertyKey, null);
        };
    }
  • 使用场景

  跟@Autowired使用场景一样

@Qualifier

  • 代码实现
export function Qualifier(name: string): Function {
    // console.log('Qualifier >> ', name);
    return (classPrototype: any, methodOrAttributeName: string, index: number) => {
        const constructor: any = typeof classPrototype == "function" ? classPrototype : classPrototype.constructor;
        let props: any;

        if (typeof index === "number") {
            // it's a parameter on a method
            let methodName: string;
            props = getOrCreateProps(constructor);
            if (methodOrAttributeName) {
                methodName = methodOrAttributeName;
            } else {
                methodName = "__constructor";
            }
            const autowireMethods = 'autowireMethods';
            if (!props[autowireMethods]) {
                props[autowireMethods] = {};
            }
            if (!props[autowireMethods][methodName]) {
                props[autowireMethods][methodName] = {};
            }
            props[autowireMethods][methodName][index] = name;
        }
    };
}
  • 使用场景
export class LoggerFactory {

    private logging: boolean;

    private setBeans(@Qualifier('optionsWrapper') optionsWrapper: OptionsWrapper): void {
        this.logging = optionsWrapper.isDebug();
    }

    public create(name: string) {
        return new Logger(name, this.isLogging.bind(this));
    }

    public isLogging(): boolean {
        return this.logging;
    }
}

具体代码请参考:https://github.com/FxLsoft/summer

 

posted @ 2021-08-13 17:58  FxLsoft  阅读(206)  评论(0)    收藏  举报