A simple @Inject only works inside a bundle.
If you want to inject an object from another bundle then Export the object as a service in bundle B and inject it as a service in bundle C.

Btw. using the blueprint-maven-plugin you can even use the CDI/JEE annotations for blueprint.

See this to inject a service:
https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-blueprint-cdi/tasklist-service/src/main/java/net/lr/tasklist/service/impl/TaskServiceRest.java#L31

and this to offer a service:
https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-blueprint-cdi/persistence/src/main/java/net/lr/tasklist/persistence/impl/TaskServiceImpl.java#L18

The approach works with both pax-cdi and blueprint with the blueprint-maven-plugin.

Having said this I support what Achim said. In many cases it is even better to use declarative services instead as they are better tailored for OSGi. In your case CDI might be the better choice as you have more experience with it but I would experiment with both.

Christian

On 06.03.2017 11:04, erwan wrote:
Hello,
I'm new to osgi world (coming from a JEE bck) and trying to use pax-cdi at
first to manage injection.
I have 3 bundles
Bundle A declares an interface:
/package com.eg;
public interface Itf {
        public void PrintITf();

}/

with Manifest generation using maven:
<Export-Package>com.eg*</Export-Package>


Bundle B implements this interface:
/package com.eg.impl;
public class ItfImpl implements Itf {

        public void PrintIFy() {
                System.out.println("Hello");
        }
}/

<Import-Package>
com.eg*,*
</Import-Package>

Bundle C is trying to inject Itf (as it can be done using JEE):
/@Inject
        private Itf itf;
        public void setItf(Itf itf) {
                this.itf= itf;
        }
        
        public Use() {
                itf.PrintIFy();
        }/
<Import-Package>
com.eg*, com.eg.impl*, *
</Import-Package>
<Require-Capability>
osgi.extender;
filter:="(osgi.extender=pax.cdi)",
org.ops4j.pax.cdi.extension;
filter:="(extension=pax-cdi-extension)"
</Require-Capability>

It doesn't seem to work as I always get NullPointerException starting bundle
C.

is this a classical use case in OSGI framework?
Do you think I should change the way I split these 3 bundles (moving
interface and implementation in the same bundle)?

I also tried with blueprint without being able to do it as well...
thanks for your help



--
View this message in context: 
http://karaf.922171.n3.nabble.com/Multiple-bundles-dependencies-injection-pax-cdi-or-blueprint-tp4049756.html
Sent from the Karaf - User mailing list archive at Nabble.com.


--
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
http://www.talend.com

Reply via email to