Search code examples
javareflectiondependency-injectioninversion-of-control

DI and IOC using core java


Is there a way to use core java/reflection to load create dependency objects and set it to other as a dependency?

I could do it, but the facing issues while dealing with nested beans.

Ultimately, I am not interested to use spring for the simple usage.

Any help much appreciated.


Solution

  • I have written it myself.

    Couple of pre-requisites though

    1. Format your xml to reduce the load on parser. I have defined xml element <bean id="xyz" class="package path of class"> for declaring a class, and <parameter name="xyz" value="val"> or <parameter name="xyz" ref="beanid"> for instance variables. Parameter can point to a value or reference of another object
    2. On the parser side, all beans are created using reflection and posted into hashmap, from there dependency can be injected using parameters either by reference or by value

    Pretty simple. Thanks for all comments