diff --git a/examples/simple_async_init/bus.js b/examples/simple_async_init/bus.js new file mode 100644 index 0000000..dcb1c53 --- /dev/null +++ b/examples/simple_async_init/bus.js @@ -0,0 +1,15 @@ +var Bus = function() {} + +Bus.prototype.init = function(cb) { + console.log('init Bus...'); + setTimeout(function() { + console.log('Bus asyncInit setTimeout'); + cb(); + }, 500); +} + +Bus.prototype.run = function() { + console.log('run bus...'); +} + +module.exports = Bus; \ No newline at end of file diff --git a/examples/simple_async_init/context.json b/examples/simple_async_init/context.json index 9654ed1..6dc4989 100644 --- a/examples/simple_async_init/context.json +++ b/examples/simple_async_init/context.json @@ -4,12 +4,22 @@ "id": "car", "func": "car", "init": "init", - "order": 2 + "order": 1, + "props": [{ + "name": "wheel", + "ref": "wheel" + }] }, { "id": "wheel", "func": "wheel", "async": true, "init": "init", - "order": 1 + "order": 2 + }, { + "id": "bus", + "func": "bus", + "async": true, + "init": "init", + "order": 3 }] } \ No newline at end of file diff --git a/lib/beans/beanFactory.js b/lib/beans/beanFactory.js index f59384a..3c91617 100644 --- a/lib/beans/beanFactory.js +++ b/lib/beans/beanFactory.js @@ -524,11 +524,16 @@ BeanFactory.prototype.preInstantiateSingletons = function(cb) { var beanDefinition = beanDefinitionOrderList[index++]; var beanName = beanDefinition.getBeanName(); if (beanDefinition.isAsyncInit()) { - var initCb = function() { - next(); + if (!self.singletonBeanFactory.containsSingleton(beanName)) { + var initCb = function() { + next(); + } + self.setInitCb(beanName, initCb); + self.getBean(beanName); + } else { + self.getBean(beanName); + next() } - self.setInitCb(beanName, initCb); - self.getBean(beanName); } else { var initCb = function() {} self.setInitCb(beanName, initCb); diff --git a/lib/beans/singletonBeanFactory.js b/lib/beans/singletonBeanFactory.js index 5fd3ca7..dbab9d7 100644 --- a/lib/beans/singletonBeanFactory.js +++ b/lib/beans/singletonBeanFactory.js @@ -43,7 +43,7 @@ SingletonBeanFactory.prototype.addSingleton = function(beanName, beanObject) { * @api public */ SingletonBeanFactory.prototype.containsSingleton = function(beanName) { - return this.singletonObjects[beanName] !== null; + return !!this.singletonObjects[beanName]; } /** diff --git a/package.json b/package.json index c85f465..ee746bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bearcat", - "version": "0.1.1", + "version": "0.1.2", "description": "a POJOs based application framework for node.js", "main": "index.js", "scripts": { diff --git a/test/context/debug.js b/test/context/debug.js index d61d9c0..09a0851 100644 --- a/test/context/debug.js +++ b/test/context/debug.js @@ -8,21 +8,14 @@ var ApplicationContext = require('../../lib/context/applicationContext'); // var car = applicationContext.getBean('car'); // var r = car.run(); -var simplepath = require.resolve('../../examples/placeholder/context.json'); +var simplepath = require.resolve('../../examples/simple_async_init/context.json'); var paths = [simplepath]; -var path = require('path'); - -var configPath = path.dirname(simplepath) + '/config'; var applicationContext = new ApplicationContext(paths); -applicationContext.setConfigPath(configPath); -applicationContext.refresh(); - -var car = applicationContext.getBean('car'); -var r = car.run(); +applicationContext.on('finishRefresh', function() { + var car = applicationContext.getBean('car'); + var r = car.run(); -applicationContext.setEnv('prod'); -applicationContext.refresh(); +}); -car = applicationContext.getBean('car'); -r = car.run(); \ No newline at end of file +applicationContext.refresh(); \ No newline at end of file