Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » @Optional doesn't work as I expect
@Optional doesn't work as I expect [message #731600] Sun, 02 October 2011 00:03 Go to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 18
Registered: September 2011
Junior Member
I've got a method:

class MyClass {

...

@Inject
void setSomething(@Optional Something something) {
// do something with something
}

}

I want this method to be called only if I've placed something in the context to be injected. Trouble is, when I construct a MyClass, a Something gets constructed to satisfy the injection. That's not what I want. How can I make this truly optional so that DI won't default construct something in order to call it? I'd settle for a call to setSomething(null).
Re: @Optional doesn't work as I expect [message #731635 is a reply to message #731600] Sun, 02 October 2011 07:15 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

I also saw this "strange" behavior when Something has a default
constructor. Is this the case for you as well? I need to talk to Oleg to
find out why this happens and will follow up with you one I know more.

Tom

Am 02.10.11 02:03, schrieb grillo321:
> I've got a method:
>
> class MyClass {
>
> ..
>
> @Inject
> void setSomething(@Optional Something something) {
> // do something with something
> }
>
> }
>
> I want this method to be called only if I've placed something in the
> context to be injected. Trouble is, when I construct a MyClass, a
> Something gets constructed to satisfy the injection. That's not what I
> want. How can I make this truly optional so that DI won't default
> construct something in order to call it? I'd settle for a call to
> setSomething(null).
Re: @Optional doesn't work as I expect [message #731754 is a reply to message #731600] Sun, 02 October 2011 23:02 Go to previous message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 18
Registered: September 2011
Junior Member
Thanks for the reply Tom. Of course now I can't reproduce the problem, but I think the times I've seen it have involved a default constructor or at least a constructor that can be injected successfully.

And I've got another question about how @Optional is supposed to work.

If I write:

@Inject
@Optional
void someFunc(SomeClass someThing) {
...
}

I would expect that the call wouldn't be made if there's no SomeClass in the context. What I see is that the call is made but null is passed in. This is the same result as putting the @Optional on the parameter. Or putting it in both places for that matter.

Is this working as intended?
What's the best place to find doc on how this should work?
Previous Topic:When Processor method is called ?
Next Topic:Change Part size dynamically
Goto Forum:
  


Current Time: Fri Apr 26 23:25:46 GMT 2024

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

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

Back to the top