In JavaEE6, we can make good use of IoC through various annotations (@EJB, @Resource, …).
But, is it possible to obtain an instance of the server component used to instanciate those objects ? What for ? Well, in some case, i want to be able to instanciate objects (of random classes) that may contain @EJB and other similar references. To ease my job, it would be great if I could use the component used by the server to instanciate my application, as I wouldn’t have to configure myself usable classpath and other such elements.
To instanciate objects in CDI, one should not try to find instanciator object (named
BeanManager), but rather access to already defined instances, which areInstanceobjects. As an example, to access the generic instance, one can simply define aninstancevariable this way :Then use it anywhere in the code to instanciate a random class :