I like google-guice very much so far, especially for its simplicity,
expressiveness and that it can be used flexibly in many different
types of applications.
One thing that I either didn't figure out or that is missing is how to
do the initialization of an Object that uses guice's injection. Let me
try to clarify what I mean: In a POJO you would do all initialization
of an Object in its constructor. However, that pattern doesn't work
when using injection, since some initialization might depend on
properties that are being injected and thus are not available during
the constructor-call.
When all injections are done in the constructor this is not an issue.
But when property or method is used, the constructor cannot serve as a
place for initialization any more.
One dirty workaround that I figured out is to have a function like
"@Inject void init() { /* init stuff */ }" that is textually after all
other injection functions. However this is obviously a hack, since it
relies on the textual order of the functions.
What I imagine would be a nice solution is to have an annotation (I'll
call it "@Initialize") that can be used on a method. That method will
be called after all dependencies have been injected. Thus:
"@Initialize void init() { /* init stuff */ }"
Inheritance complicates the order of initialization a bit, but should
still be obvious.
what do you think?
so long,
Christoph
Currently, you pretty much need to include all of your required dependencies in the constructor and put your initialization logic there. You can put some logic in an injected method, but you cannot depend on the ordering of injected methods (Java explicitly doesn't guarantee that the runtime order will match the order in your source file).
The next version of Guice will handle this much better.
Out of curiosity: How will the next version of Guice handle that? And
how long have we to wait for the next version? There are some
improvements announced for 1.1 which would be very helpful for our
project ...
On 23 Mai, 02:00, "Bob Lee" <crazy...@crazybob.org> wrote:
> Currently, you pretty much need to include all of your required dependencies
> in the constructor and put your initialization logic there. You can put some
> logic in an injected method, but you cannot depend on the ordering of
> injected methods (Java explicitly doesn't guarantee that the runtime order
> will match the order in your source file).
>
> The next version of Guice will handle this much better.
>
> Bob
>
> On 5/22/07, christoph.die...@googlemail.com <christoph.die...@googlemail.com>
At least you know that a method injection is always called after a
constructor injection. Thats the way we are doing our initialization:
One injected constructor and one injected void init() method for the
initialization.
In many cases the initialization depends on the internal state of
other injected instances. But you can not call methods on injected
members, they are still just proxies. So calling init from the
constructor wont help.
I also dont like the way it is now, but it works. The alternative
could be to write Factories/Builders for all these classes that need
an initialization and thats a lot of overhead for a simple thing like
a "delayed" initialization. On the other hand, you are not able to
retrieve the instances of these (injected) Factories/Builders in your
injected constructor and there is the same problem again.
On 23 Mai, 10:20, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> That sounds like a terrible hack. =(
> Why not just call init() from the ctor?
In many cases the initialization depends on the internal state of
other injected instances.