Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » e4 RCP Dependency Injection Hierarchy Problems?(An injected object always has null for @Injected variable)
e4 RCP Dependency Injection Hierarchy Problems? [message #1702362] Tue, 21 July 2015 19:36 Go to next message
Dominic Hilsbos is currently offline Dominic HilsbosFriend
Messages: 9
Registered: May 2012
Junior Member
I'm writing an e4 RCP application and I've run into a little snag with significant potential impact, and I'm hoping that it's something small that I'm missing.

The long and the short of it is: it looks like dependency injection (via. @Inject) only works for objects that are part of the GUI.

I've prepared a project set that illustrates the issues I have, but I will also post code snippets.

This is the code from TestPart.java:
package com.performair.test.rpc;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import com.performair.test.bl.IBL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class TestPart
{
	@Inject
	private IBL bl;
	
	@PostConstruct
	public void postConstruct(Composite parent)
	{
		List<String> strings;
		Label lblTemp;
		
		strings = bl.get();
		
		parent.setLayout(new GridLayout(1, true));
		
		for (String string: strings)
		{
			lblTemp = new Label(parent, SWT.NONE);
			lblTemp.setText(string);
		}
	}
}


A BLImplementation object is injected at line 18 just fine. The code for BLImplementation.java is:
package com.performair.test.bl.implementation;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.performair.test.bl.IBL;
import com.performair.test.db.IDB;

public class BLImplementation
	implements IBL
{
	@Inject
	private IDB db;
	
	protected void activate()
	{
		System.out.println("In BLImpementation.activate()");
	}
	
	@Override
	public List<String> get()
	{
		System.out.println("In BLImpementation.get()");
		if (db == null)
		{
			System.out.println("db is null");
			
			List<String> strings = new ArrayList<>(1);
			
			strings.add("Fail.");
			
			return strings;
		}
		else
		{
			return db.get();
		}
	}
}


The problem is at line 15 of this class; the IDB instance is never injected (db is always null), and no error is generated.
The IDB implementation (DBImplementation.java) looks like this:
package com.performair.test.db.implementation;

import java.util.LinkedList;
import java.util.List;

import com.performair.test.db.IDB;

public class DBImplementation
	implements IDB
{
	protected final void activate()
	{
		System.out.println("DBImplementation activate() called.");
	}
	
	@Override
	public List<String> get()
	{
		List<String> lReturn;
		
		System.out.println("In DBImplementation get().");
		
		lReturn = new LinkedList<>();
		
		lReturn.add("One");
		lReturn.add("Two");
		lReturn.add("Three");
		lReturn.add("Four");
		lReturn.add("Five");
		
		return lReturn;
	}
}


I can't post the component.xml contents here, so you'll have to look it up in the attached .zip file.

The ss command at the osgi console indicates that both com.performair.test.bl.implementation and com.performair.test.db.implementation are ACTIVE.

Can anyone point me in the right direction?

I've been working through Eclipse 4 RCP by Lars Vogel, and OSGi and Equinox by Jeff McAffer and Paul VanderLei as well as searching forums across the web (including this one).

Thank you for taking the time to read my quest.

Dominic Hilsbos
Re: e4 RCP Dependency Injection Hierarchy Problems? [message #1702363 is a reply to message #1702362] Tue, 21 July 2015 19:50 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Is BLImplementation an OSGi declarative service? Then you can't use dependency injection. Instead your dB implementation should also be an OSGi service that is referenced.
Re: e4 RCP Dependency Injection Hierarchy Problems? [message #1702365 is a reply to message #1702363] Tue, 21 July 2015 19:57 Go to previous messageGo to next message
Dominic Hilsbos is currently offline Dominic HilsbosFriend
Messages: 9
Registered: May 2012
Junior Member
Dirk: Thank you for responding so quickly.

First off:
"Is BLImplementation an OSGi declarative service?"
Yes, BLImplementation is an OSGi declarative service.

I guess I'm going to show my ignorance here...

Why can't a declarative service use dependency injection?

"Instead your dB implementation should also be an OSGi service that is referenced."
Where and / or how is another DS referenced?

How do I get an instance of IDB, without worrying about which implementation of IDB I'm getting (if not through DI)?

Thank you for taking the time to read my reply,
Dominic Hilsbos
Re: e4 RCP Dependency Injection Hierarchy Problems? [message #1702367 is a reply to message #1702365] Tue, 21 July 2015 20:23 Go to previous message
Dominic Hilsbos is currently offline Dominic HilsbosFriend
Messages: 9
Registered: May 2012
Junior Member
I found the answer to the follow up questions from Dirk's post.

I'd share the URL, but apparently I can't...
This information came from slides 10 and 13 of a slideshare titled "Declarative Services Dependency Injection OSGi style"

Essentially, I need to add the relationship to the BLImpelementation's component.xml, with a bind=<method>, the <method> will be used to pass an instance to my service.

Again, thank you Dirk for pointing me in the right direction.
Previous Topic:Refresh model after loading a fragment from code
Next Topic:execute custom action on closing of an MPartStacks MPart
Goto Forum:
  


Current Time: Thu Apr 25 11:41:42 GMT 2024

Powered by FUDForum. Page generated in 0.03792 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top