UNPKG

node-web-mvc

Version:
90 lines (89 loc) 3.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Component_1 = __importDefault(require("../../ioc/annotations/Component")); const BeanDefinition_1 = __importDefault(require("../../ioc/factory/BeanDefinition")); const Scope_1 = __importDefault(require("../annotations/Scope")); const ElementType_1 = __importDefault(require("../annotations/annotation/ElementType")); const RuntimeAnnotation_1 = __importDefault(require("../annotations/annotation/RuntimeAnnotation")); const hot_update_1 = __importDefault(require("./hot-update")); class AbstractApplicationContext { constructor() { // 注册热更新 (0, hot_update_1.default)(this.getBeanFactory.bind(this), this.registerWithAnnotation.bind(this), this.createSingletonBeans.bind(this)); } prepareBeanFactory() { } /** * 根据注解注册Bean定义 * @param annotation Component注解 */ registerWithAnnotation(annotation) { var _a; const clazz = annotation.ctor; const scopeAnno = RuntimeAnnotation_1.default.getClassAnnotation(clazz, Scope_1.default); const name = annotation.nativeAnnotation.value; const scope = (_a = scopeAnno === null || scopeAnno === void 0 ? void 0 : scopeAnno.nativeAnnotation) === null || _a === void 0 ? void 0 : _a.scope; const definition = new BeanDefinition_1.default(clazz, null, scope); const beanFactory = this.getBeanFactory(); const clazzBeanName = BeanDefinition_1.default.toBeanName(definition.clazz); if (name && name !== clazzBeanName) { // 注册自定义名称 beanFactory.registerBeanDefinition(name, definition); } // 根据类名注册 beanFactory.registerBeanDefinition(clazzBeanName, definition); return definition; } /** * 扫描注册所有bean */ registerAllComponentBeans(fallback = false) { const beanFactory = this.getBeanFactory(); const annotations = RuntimeAnnotation_1.default.getAnnotations(Component_1.default); // 这里需要排除作用在同一个类上的多个component,防止重复注册 const map = new Map(); const singleAnnotations = []; annotations.forEach((annotation) => { if (annotation.elementType == ElementType_1.default.TYPE && !map.has(annotation.ctor)) { map.set(annotation.ctor, true); singleAnnotations.push(annotation); } }); singleAnnotations.forEach((annotation) => { const beanName = BeanDefinition_1.default.toBeanName(annotation.ctor); switch (annotation.elementType) { case ElementType_1.default.TYPE: if (fallback && beanFactory.getBeanDefinition(beanName)) { // 在fallback模式下,如果已存在,则跳过 return; } this.registerWithAnnotation(annotation); break; } }); } createSingletonBeans() { const beanFactory = this.getBeanFactory(); const keys = beanFactory.getBeanDefinitionNames(); for (const key of keys) { const definition = beanFactory.getBeanDefinition(key); if (definition.scope == 'singleton') { beanFactory.getBean(key); } } } onFinish() { } refresh() { this.prepareBeanFactory(); this.registerBeanPostProcessor(); this.registerAllComponentBeans(); this.createSingletonBeans(); this.registerAllComponentBeans(true); this.onFinish(); } } exports.default = AbstractApplicationContext;