Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » DI in activator with Compatibility layer
DI in activator with Compatibility layer [message #1084910] Mon, 12 August 2013 07:52 Go to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
Hello

I'm modifying an e3 App to work on e4.
i already wrote my new code which uses DI (works fine if called via handler behind an menuitem)

now i want a custom class to be registered (to be able to DI it) when i launch my application.

therefore i wrote the following in my Activator

public class Activator implements BundleActivator, ServiceTrackerCustomizer {

		BundleContext bundleContext;
		ServiceTracker emfTracker;
		private IEclipseContext eclipseContext;

		public void start(BundleContext bundleContext) throws Exception {
				this.bundleContext = bundleContext;
				System.out.println("Gemini JPA Basic Sample started");
				this.eclipseContext = EclipseContextFactory.getServiceContext(bundleContext);
				DAO qo = ContextInjectionFactory.make(DAO.class, eclipseContext);
				...
		}
		...
}


the DAO itself looks like this:
@Creatable
public class DAO {

	@Inject
	@Preference
	IEclipsePreferences preferences;

	@Inject
	public DAO() {
		preferences.put(*/put something here*/);
		try {
			preferences.flush();
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
	}
}


the exception raised is:
Quote:
Caused by: java.lang.NullPointerException
at at.biooffice.osgi.service.eclipselink.impl.DAO.<init>(DAO.java:42)

this means the preferences is null (not injected)

when i call the DAO via a handler (which natively uses e4 injection and call the DAO, this works.)

note: i followed the instructions from Vogella
Re: DI in activator with Compatibility layer [message #1084927 is a reply to message #1084910] Mon, 12 August 2013 08:18 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Same error as last time! You are accessing an injected field in the
constructor this can/will never ever work, no matter how hard you try it ;-)

Tom

On 12.08.13 09:52, Ludwig Moser wrote:
> Hello
>
> I'm modifying an e3 App to work on e4.
> i already wrote my new code which uses DI (works fine if called via
> handler behind an menuitem)
>
> now i want a custom class to be registered (to be able to DI it) when i
> launch my application.
>
> therefore i wrote the following in my Activator
>
> public class Activator implements BundleActivator,
> ServiceTrackerCustomizer {
>
> BundleContext bundleContext;
> ServiceTracker emfTracker;
> private IEclipseContext eclipseContext;
>
> public void start(BundleContext bundleContext) throws Exception {
> this.bundleContext = bundleContext;
> System.out.println("Gemini JPA Basic Sample started");
> this.eclipseContext =
> EclipseContextFactory.getServiceContext(bundleContext);
> DAO qo = ContextInjectionFactory.make(DAO.class,
> eclipseContext);
> ...
> }
> ...
> }
>
> the DAO itself looks like this:
> @Creatable
> public class DAO {
>
> @Inject
> @Preference
> IEclipsePreferences preferences;
>
> @Inject
> public DAO() {
> preferences.put(*/put something here*/);
> try {
> preferences.flush();
> } catch (BackingStoreException e) {
> e.printStackTrace();
> }
> }
> }
>
> the exception raised is:
> Quote:
>> Caused by: java.lang.NullPointerException
>> at at.biooffice.osgi.service.eclipselink.impl.DAO.<init>(DAO.java:42)
>
> this means the preferences is null (not injected)
>
> when i call the DAO via a handler (which natively uses e4 injection and
> call the DAO, this works.)
>
> note: i followed the instructions from
> http://www.vogella.com/articles/EclipseRCP/article.html#ownobjects
Re: DI in activator with Compatibility layer [message #1084937 is a reply to message #1084927] Mon, 12 August 2013 08:35 Go to previous messageGo to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
so i need to do it like this?

@Creatable
public class DAO {

	@Inject
	public DAO([b]@Preference[/b] IEclipsePreferences preferences) {
		preferences.put(*/put something here*/);
		try {
			preferences.flush();
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
	}
}


EDIT: my mistake was the missing @Preference in the constructor
Thanks Tom!

one thing is still missing... when i call a function on my class, which also has inected fields. the fields are null. afaik the fields are injected right after the constructor, so when i call a function after construction the fields should be already available to this function, right?

[Updated on: Mon, 12 August 2013 09:52]

Report message to a moderator

Re: DI in activator with Compatibility layer [message #1084997 is a reply to message #1084937] Mon, 12 August 2013 10:00 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Yes
On 12.08.13 10:35, Ludwig Moser wrote:
> from the other post:
> Quote:
>> This is not correct you can inject values in the constructor - as part
>> of the argument list to the constructor call.
>
>
> so i need to do it like this?
>
> @Creatable
> public class DAO {
>
> @Inject
> public DAO(IEclipsePreferences preferences) {
> preferences.put(*/put something here*/);
> try {
> preferences.flush();
> } catch (BackingStoreException e) {
> e.printStackTrace();
> }
> }
> }
Re: DI in activator with Compatibility layer [message #1085005 is a reply to message #1084997] Mon, 12 August 2013 10:19 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
BTW in 4.3 there is a better way to get to the IEclipseContext!

You should get the IWorkbench from the OSGi-Service registry and there
you call getApplication().getContext()

Tom

On 12.08.13 12:00, Tom Schindl wrote:
> Yes
> On 12.08.13 10:35, Ludwig Moser wrote:
>> from the other post:
>> Quote:
>>> This is not correct you can inject values in the constructor - as part
>>> of the argument list to the constructor call.
>>
>>
>> so i need to do it like this?
>>
>> @Creatable
>> public class DAO {
>>
>> @Inject
>> public DAO(IEclipsePreferences preferences) {
>> preferences.put(*/put something here*/);
>> try {
>> preferences.flush();
>> } catch (BackingStoreException e) {
>> e.printStackTrace();
>> }
>> }
>> }
>
Previous Topic:Reload application model at runtime
Next Topic:Problem with selectionChanged
Goto Forum:
  


Current Time: Tue Mar 19 06:54:50 GMT 2024

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

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

Back to the top