Is it possible in a JavaEE6 application server to obtain a reference to the "instanciator" component
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 con开发者_如何学JAVAtain @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 are Instance
objects. As an example, to access the generic instance, one can simply define an instance
variable this way :
@Inject private Instance<?> instance;
Then use it anywhere in the code to instanciate a random class :
MyEJB ejb = instance.select(MyEJB.class).get();
精彩评论