mardi 29 avril 2014

Java - CDI à l'intérieur de l'OSGI - il ne voit pas de haricot - Stack Overflow


I have two OSGI bundles:
The first contains: service, servlet, pojo
The second contains: consumer (of the service).


FACTS:
A) I inject service (bundle 1) to consumer (bundle 2) via CDI (@OSGiService) - everything is OK.
B) I inject pojo to servlet via CDI (manually using beanManager.getBeans,beanManager.resolve) - everything is OK.
C) I inject pojo to service via CDI (manually using beanManager.getBeans,beanManager.resolve) - Error - can't locate the bean.


The reason why I use manual injection is that that I couldn't get it to work with @Inject and started googling. And I found this



CDI allows injection into non-component classes, but the instances must still be created by CDI. Your activator is instantiated by OSGi framework and CDI has no way to hook into that process. You can programmatically get hold of the CDI injection manager and request an instance from it instead of using @Inject in your main class.



I think that (C) must work, because instance is created via CDI but it doesn't. Where is mistake?


EDIT: Glassfish4, weld, apache felix


Source code of 3 bundles (1 bundle with interface) can be downloaded here. There are two servlets:
The first one can be accessed /testme1/NewServlet1 (bundle 1)
The second one can be accessed /testme2/NewServlet2 (bundle 2)



I have two OSGI bundles:
The first contains: service, servlet, pojo
The second contains: consumer (of the service).


FACTS:
A) I inject service (bundle 1) to consumer (bundle 2) via CDI (@OSGiService) - everything is OK.
B) I inject pojo to servlet via CDI (manually using beanManager.getBeans,beanManager.resolve) - everything is OK.
C) I inject pojo to service via CDI (manually using beanManager.getBeans,beanManager.resolve) - Error - can't locate the bean.


The reason why I use manual injection is that that I couldn't get it to work with @Inject and started googling. And I found this



CDI allows injection into non-component classes, but the instances must still be created by CDI. Your activator is instantiated by OSGi framework and CDI has no way to hook into that process. You can programmatically get hold of the CDI injection manager and request an instance from it instead of using @Inject in your main class.



I think that (C) must work, because instance is created via CDI but it doesn't. Where is mistake?


EDIT: Glassfish4, weld, apache felix


Source code of 3 bundles (1 bundle with interface) can be downloaded here. There are two servlets:
The first one can be accessed /testme1/NewServlet1 (bundle 1)
The second one can be accessed /testme2/NewServlet2 (bundle 2)


0 commentaires:

Enregistrer un commentaire