Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Is deferring method injection after PostConstruct possible?
Is deferring method injection after PostConstruct possible? [message #1807684] Thu, 06 June 2019 06:37 Go to next message
Guido Schnepp is currently offline Guido SchneppFriend
Messages: 13
Registered: March 2013
Junior Member
Hi all,

I have a specific problem with dependency injection order in my e4 RCP application. Hope you have an answer for me on how to proceed here:

Being currently designed as a multi-perspective application I'm trying to handle over a setting, basically the selected file, from one perspective to another using DI for this and an own object. I use the MTrimmedWindow context for this, being available in all sub-perspectives of that window. This works great! But ...
Dependency injection order* is a little problem here since my @Inject annotated receiving method runs before @PostConstruct was able to initialize the necessary widgets like a TreeViewer an others. This is true for the first selection of the target perspective only, which means perspective's parts are initialized on first invocation/presentation (which is ok) but DI happens in "wrong" order in this case.

I can imagine different solutions:
1. Create parts in target perspective early so that PostConstruct is run before object is set to context (how?)
2. Defer method injection after PostConstruct (how?)
3. ...
4. Catch and store object in method, look, if target widgets have been created already (= PostConstruct is run) and proceed either immediately or a the end of PostConstruct (by hand, on the stored object).

No. 4 is my current solution but far from being the best one, I assume, since I loose the benefits from generic context injection a little bit.

On a completely different matter: Using perspectives and a self-made perspective switcher may not be the best design decision here, but the most logical to me: How would you solve a RCP application with different (full-size) views to change between via view selector? A single-perspective app with top-level part stack would have the same problem here I think, that inner parts are initialized not before first invocation.

Many thanks in advance!

Re: Is deferring method injection after PostConstruct possible? [message #1807687 is a reply to message #1807684] Thu, 06 June 2019 07:47 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi Guido,

we use efxclipse [1] on top of e4. They have some nice features. One feature that can help you here ist the ContextBoundValue:

This is an object you can inject and then attach a listener to it if you want to react to value changes in the IEclipseContext. So you would inject this in your PostConstruct and immediately use it the initialize your ui. You would also attach a listener, to update the ui when the value changes.

void postContruct(@ContextValue(value="theSelectedFile") ContextBoundValue<File> selectedFileCBV) {
	// create UI

	// Initialize with currently selected File
	File selectedFile = selectedFileCBV.getValue();
	// Don't forget to dispose the Subscription on @PreDestroy
	Subscription subscription = selectedFileCBV.subscribeOnValueChange(file -> setSelectedFile(file));

The ContextBoundValue is also very helpful to publish values to the IEclipseContext [2].

Side note: efxclipse is mostly known for its JavaFX renderers. AFAIK you do not need to use those in order to use the low level stuff like ContextBoundValue which is in the bundle org.eclipse.fx.core.di and has no dependencies on JavaFX.

Hope this helps.


Re: Is deferring method injection after PostConstruct possible? [message #1810166 is a reply to message #1807687] Fri, 02 August 2019 06:42 Go to previous message
Guido Schnepp is currently offline Guido SchneppFriend
Messages: 13
Registered: March 2013
Junior Member

thank you! That helps understanding it more. Well, I have another, multi-window/multi-perspective related problem with no real solution acutally, but E(fx)clipse seems to have no solution for this that time:

Thanks again
Previous Topic:Sending events to single window only
Next Topic:Problem using new transient ui element feature
Goto Forum:

Current Time: Mon Aug 03 12:52:13 GMT 2020

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

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

Back to the top