Method Injection Example

4,567 views
Skip to first unread message

Adam Schaible

unread,
Nov 9, 2007, 3:32:14 PM11/9/07
to google-guice
Hello,

I'm a bit confused about method injection.

@Inject
public String do(@Named("something")String something) {
return something;
}

So this makes sense to me..

But when I call the method, what arguments do I pass when calling the
method?

I'm probably not using it correctly!

Thanks

Martin Lukasiewycz

unread,
Nov 9, 2007, 4:53:54 PM11/9/07
to google-guice
Hi Adam,

actually you will never call this method. This is the task of guice.
That means in an Module you will bind a String constant like the
following:

bindConstant().annotatedWith(named("something")).to("Hi, Adam");

And when guice creates your instance of your class the method do is
called with the argument something="Hi Adam".
That mean also, that the method should be 'void' because only guice
will get the return value and not you.

If youre not sure if youre using guice correctly i would recommend you
the user guide, its quite short and simple to understand.

ciao
Martin

Dhanji R. Prasanna

unread,
Nov 9, 2007, 6:06:58 PM11/9/07
to google...@googlegroups.com
This highlights a bit of confusion caused by nomenclature. "Method
Injection" is not really what this is, it is setter injection (type
2).

Method Injection is what the spring folks call AOP injection where a
method invocation is decorated with parameters on the fly. I suggest
we stick to the well-known "setter" injection and explain that it
doesn't have to mean property injection, i.e. setBlah(). My 2c.

Dhanji.

Adam Schaible

unread,
Nov 9, 2007, 8:33:06 PM11/9/07
to google-guice
Yes definately, it was just a nomenclaure issue.

Thanks Dhanji !

> > > Thanks- Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages