Without depending from third-party frameworks, I would like to have a static context class to inject dependencies across different packages of my application. This way, I could avoid to pass the reference to a context object through several calls.
I have been looking at the standard Java APIs and found out about the javax.naming.Context
interface and its implementation javax.naming.InitialContext
. These can be used for dependency injection, but the context object would be non-static.
Basically, I would like to set
a reference to an object from one package and get
the same reference from another one. In other words, I would have a similar call in class (or package) A:
MyContext.setInstanceOf(MyInterface.class, new MyInterfaceImpl());
And the corresponding call in class (or package) B:
MyInterface obj = (MyInterface) MyContext.getInstanceOf(MyInterface.class);
Thanks.