Skip to content

Function annotation di

fantasyni edited this page Jul 23, 2014 · 3 revisions

Overview

It is not easy when using configuration json files based dependency injection, especially when the number of beans is growing larger and larger, developer has to update the configuration json files to express the dependencies to bearcat, it will be hard to maintain and easy to make mistakes.
The one way to solve this is to move the configuration meta into POJO files, like:

code-based meta
car.js

var Car = function() {}

Car.prototype.run = function() {
	console.log('run car...');
	return 'car';
}

// func is the constructor function
module.exports = {
	id: "car",
	func: Car
};

This can resolve maintaining configuration json files, but what the POJO exports is required to a configuration object, it is indirectly required for bearcat environment, without bearcat, this codes may hard to be ran well.

$ based name parameter configuration

Quick startup

Using $ based name parameter configuration, it is easy as writing object parameters, the only difference is the head of parameter name must be $

car.js

var Car = function($engine) {
	this.$id = "car";
	this.$scope = "prototype";
	this.$engine = $engine;
	this.$wheel = null;
}

Car.prototype.run = function() {
	this.$engine.run();
	var res = this.$wheel.run();
	console.log('run car...');
	return 'car ' + res;
}

module.exports = Car;

engine.js

var Engine = function() {
	this.$id = "engine";
}

Engine.prototype.run = function() {
	console.log('run engine...');
	return 'engine';
}

module.exports = Engine;

wheel.js

var Wheel = function() {
	this.$id = "wheel";
}

Wheel.prototype.run = function() {
	console.log('run wheel...');
	return 'wheel';
}

module.exports = Wheel;

car uses $engine in constructor function to express a constructor-based DI, $wheel expresses a object properties DI

$id specifies the unique id in bearcat for the POJO
$scope specifies the scope of the POJO

Usage

  • add $ ahead of bean attributes, configurable bean attributes:
"id", "order", "init", "destroy", "factoryBean", "factoryMethod", "scope", "async", "abstract", "parent", "lazy", "factoryArgs", "proxy", "aop"
  • constructor-based DI in constructor function arguments, the argument with $ ahead means injecting a bean, parameter string after $ specifies the bean id, arguments without $ ahead mean injecting variable, inject a value in constructor is not supported
var Car = function($engine, num) {
		
}
  • object-properties-based DI in object properties, properties with $ ahead means injecting bean, parameter string after $ specifies the bean id ,with $V ahead mean inject values, without $ ahead are normal properties
var Car = function() {
	this.$id = "car";
	this.$engine = null;
}

Note: 1: when $ ahead object properties are already in constructor arguments, it means a constructor-based DI

var Car = function($engine) {
	this.$engine = $engine; // constructor-based DI
}

2: when need to inject a namespace bean, use $N based parameter

this.$Ncar = "app:car";

now the parameter string after $ do not mean a bean id, the target ref is defined on the right after the =, with namespace:bean id

References

Note

use process.env.BEARCAT_ANNOTATION = 'off'; to shutoff bearcat use $ function annotation