Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » @Optional resource not being injected
@Optional resource not being injected [message #1062384] Fri, 07 June 2013 09:20 Go to next message
Ezequiel Panepucci is currently offline Ezequiel Panepucci
Messages: 4
Registered: June 2013
Junior Member
Dear All,

I have a resource declared as @Creatable @Singleton.

The resource is properly injected if I use @Inject.

If I use @Inject @Optional the resource is not injected.

Any clues as to what may be wrong?

Thanks in advance and cheers,
Zac

-- sw versions
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.3.el6_3.1-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)

Eclipse for RCP and RAP Developers
Version: Juno Service Release 2
Build id: 20130225-0426
Re: @Optional resource not being injected [message #1062622 is a reply to message #1062384] Mon, 10 June 2013 03:05 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1066
Registered: July 2012
Senior Member
@Optional means that the method is invoked even if there is no instance of the class you are trying to inject.

As you declared your class as @Creatable and @Singleton, why do you need the @Optional anyway? It should never happen that the injected value does not exist.
Re: @Optional resource not being injected [message #1062933 is a reply to message #1062622] Tue, 11 June 2013 09:07 Go to previous messageGo to next message
Ezequiel Panepucci is currently offline Ezequiel Panepucci
Messages: 4
Registered: June 2013
Junior Member
hi Dirk,

Here is the documentation for "@Optional" which I rely on:

"
The @Optional annotation can be applied to methods, fields, and parameters to mark them as optional for the dependency injection. Typically, if the injector is unable to find a value to inject, then injection will fail. However, if this annotation is specified, then:

for parameters: a null value will be injected;
for methods: the method calls will be skipped;
for fields: the values will not be injected.
"

which means that a method annotated with @Optional will be skipped not executed regardless of its existence in the context.

But in any case; I have a field annotated with @Optional and the problem is that the resource *is* available because I can inject it in a view from the same application context.

The reason I annotate this resource with @Optional is because this piece of code can execute in different applications, some of them have the resource in question but others do not have it.

I thought that @Optional was meant for this scenario...

Is there a chance this is a bug in RCP Juno?

I'll try the beta kepler later and see if that works.

Thanks for the message.

Cheers,
Zac
Re: @Optional resource not being injected [message #1062951 is a reply to message #1062933] Tue, 11 June 2013 10:12 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1066
Registered: July 2012
Senior Member
The issue is probably time related.

While the method is called the first time, the object is not created yet, therefore it is not yet available in the context. As it is marked with @Optional it will not be created.

If you inject it somewhere else without @Optional it will be CREATED, ADDED to the context and available for injection. That's because of your @Creatable annotation in your object. That means, using @Optional causes that your object is not created and added to the context.

At least that is my understanding.
Re: @Optional resource not being injected [message #1062955 is a reply to message #1062933] Tue, 11 June 2013 10:19 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 589
Registered: December 2010
Senior Member

IIRC if your injection is optional than the injector will NOT auto-create the thing annotated with @Creatable. It will create it only if the injection is mandatory. If you annotate your method with @Inject @Optional then the DI container searches for an already created instance in the context hierarchy. If it is not found it says 'nevermind' and skips the method. When you annotate it just with @Inject, it creates the creatable POJO and invokes the method with the new POJO.
Re: @Optional resource not being injected [message #1062961 is a reply to message #1062955] Tue, 11 June 2013 10:28 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1066
Registered: July 2012
Senior Member
Thanks Sopot, these are the words I was searching for to explain Smile
Re: @Optional resource not being injected [message #1062972 is a reply to message #1062961] Tue, 11 June 2013 10:48 Go to previous message
Ezequiel Panepucci is currently offline Ezequiel Panepucci
Messages: 4
Registered: June 2013
Junior Member
Guys, thanks a lot! I think I now fully understand my problem.

All the best,
Zac
Previous Topic:Urgent Help needed regarding CSS mechanism in rcp application
Next Topic:Get Navigator Files
Goto Forum:
  


Current Time: Sun Apr 20 16:07:40 EDT 2014

Powered by FUDForum. Page generated in 0.01676 seconds