This is a dependency injection module
npm install ioccontainerMain file: hello-world.js
"use strict";
var ioc = require("ioccontainer");
ioc.initialize("ioc-config.json");
var hello = ioc.resolve("hello");
console.log(hello.getMessage());Application context file: ioc-config.js
{
"bean" : [
{
"id": "hello",
"class": "/path/hello.js",
"init": "initialize",
"set" : [
{ "method": "setMessage", "value": "Hello world !!!!" }
]
}
]
}Bean file: hello.js
"use strict";
var message;
var Hello = function() {
message = "";
};
Hello.prototype.initialize = function() {
};
Hello.prototype.setMessage = function(value) {
message = value;
};
Hello.prototype.getMessage = function() {
return message;
};
module.exports = new Hello();Run
node hello-world.jsset path of configuration file
retrieve class specified with id set in configuration file
{
"bean" : [
{ "id": "require", "class": "npm_module" },
{ "id": "other", "class": "other.js" },
{ "id": "json", "class": "file.json" },
{
"id": "mymodule",
"class": "module.js",
"init": "initialize",
"set" : [
{ "method": "setValue", "value": "My value" },
{ "method": "setJSON", "ref": "json" },
{ "method": "setRequire", "ref": "require" },
{ "method": "setBean", "ref": "other" }
]
}
]
}
For any javascript module declare a bean with:
unique id name
path of class
initialize method
"method": name of method for dependency injection, using "value" for string or number or "ref" for other module declared in configuration file