npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jean

v0.0.9

Published

Small IoC library for Node.js inspired by Spring Framework IoC

Downloads

2

Readme

jean.js

Build Status NPM version

jean.js is small IoC library for Node.js inspired by Spring Framework IoC container

Installation

$ npm install jean

Usage

Create some beans

myBean1.js

/**
 * @Bean('myBean1')
 */
var MyBean1 = module.exports = function() {};

MyBean1.prototype = {
    print: function() {
        console.log('hello world');
    }
};

myBean2.js

/**
 * @Bean('myBean2')
 */
var MyBean2 = module.exports = function() {};

MyBean2.prototype = {

    /**
     * @Autowired
     */
    myBean1: null,

    doWork: function() {
        this.myBean1.print();
    }
};

Initialize Context, scan for beans and run.

app.js

var JeanContext = require('jean');

var app = new JeanContext('myapp');
app.scan(__dirname, function(err) {

    // handle init error
    if (err) {
        console.log(err, err.stack);
    }

    // work with beans
    app.getBean('myBean2').doWork();

    // Call it when you want to shutdown context and all beans (see @Destroy annotation)
    app.shutdown(function(err) {
        console.log("Shutdown done");
    });

});

Annotations

This section describe currently implemented annotations

@Bean('name')

This annotation marks your constructor as a "bean", it will be automatically scanner and put into context. Please note that you should keep only one bean per file and module.exports should export the constructor of your bean.

/**
 * @Bean('myBean')
 */
var MyBean = module.exports = function() {};

MyBean.prototype = {
    // implementation
};

@Initialize

If you mark any your prototype method definition with this annotation, method would be invoked right after creating new instance of your bean and putting it in the context, but before any autowired injections. Async initializations are supported, your method will be passed with a callback, and you need to invoke it after you complete initialization.

/**
 * @Bean('myBean')
 */
var MyBean = module.exports = function() {};

MyBean.prototype = {

    /**
     * @Initialize
     */
    doInit: function(callback) {

        // emulating long initialization...
        setTimeout(function() {
            callback();
        }, 1000);
    }

};

@Destroy

If you mark any your prototype method definition with this annotation, method would be invoked on context shutdown, i.e. when calling jeanContext.shutdown(). Please note that beans in context would be destored in random order in parallel, so not make any assumptions on it.

/**
 * @Bean('myBean')
 */
var MyBean = module.exports = function() {};

MyBean.prototype = {

    /**
     * @Destroy
     */
    destroy: function(callback) {

        // emulating long destroying
        setTimeout(function() {
            callback();
        }, 1000);
    }

};

@Autowired

If you mark your prototype property with this annotation, the dependency will be automatically injected in this property. Injected bean name should be the same as your property name, othwerise you can set injected bean name by providing parameter to annotation, for example: @Autowired('myRealBeanName').

/**
 * @Bean('myBean')
 */
var MyBean = module.exports = function() {};

MyBean.prototype = {

    /**
     * @Autowired('anotherBean')
     */
    anotherBean: null,

    doWork: function() {
        this.anotherBean.doWork();
    }

};

@AfterPropertiesSet

If you mark any your prototype method definition with this annotation, method would be invoked right after resolving all autowired dependencies.

/**
 * @Bean('myBean')
 */
var MyBean = module.exports = function() {};

MyBean.prototype = {


    /**
     * @Autowired
     */
    anotherBean: null,

    /**
     * @AfterPropertiesSet
     */
    ready: function() {

        // can access another bean now
        this.anotherBean.doWork();
    
    }

};

Tests

$ sudo npm install nodeunit -g
$ npm test

Author

License

MIT