Injecting Collections and Arrays?

1,953 views
Skip to first unread message

Justin

unread,
Aug 14, 2007, 12:35:59 PM8/14/07
to google-guice
I was having trouble understanding how to inject an array or
collection into a constructor. Anyone have and example? I need to
iterate all the instances of classes that have a certain interface

Robbie Vanbrabant

unread,
Aug 14, 2007, 1:17:15 PM8/14/07
to google...@googlegroups.com
Here's how you can inject collections in 1.0:
 
public class MultiValue {
  public MultiValue(int i) {
    System.out.println(i);
  }
  public static void main(String[] args) {
    Injector i = Guice.createInjector(new Module() {
      public void configure(Binder binder) {
        binder.bind(MultiValue.class).annotatedWith(Names.named("1")).toInstance(new MultiValue(1));
        binder.bind(new TypeLiteral<List<MultiValue>>(){})
              .toProvider(new Provider<List<MultiValue>>() {
          @Inject @Named("1") MultiValue v;
          public List<MultiValue> get() {
            ArrayList<MultiValue> arrayList = new ArrayList<MultiValue>();
            arrayList.add (v);
            return arrayList;
          }
        });
      }
    });
    List<MultiValue> instance = i.getInstance(Key.get(new TypeLiteral<List<MultiValue>>(){}));
    System.out.println (instance);
  }
}
 
In your case you could perhaps inject the injector in the provider, and use findBindingsByType. I don't know if it's that what you mean.
 
Collection support is, IMHO, the worst part of Guice. Ugly syntax, and in desperate need of multibindings:
 
and you should also use this to get rid of the ugly typeliteral syntax:
 
Hope this helps,
Robbie
Message has been deleted

Justin

unread,
Aug 14, 2007, 1:23:59 PM8/14/07
to google-guice
Ouch, that's ugly. How about arrays? Is that supported? Like Service[]
services;?

On Aug 14, 12:17 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:

Message has been deleted

Robbie Vanbrabant

unread,
Aug 14, 2007, 1:37:43 PM8/14/07
to google...@googlegroups.com
That would get rid of the TypeLiteral stuff. But if you use the issue 123 factory method it's basically the same.
Depending on when or why you need the list of classes, you could perhaps also use AOP. What's the use case?
 
Robbie
 
On 8/14/07, Justin <rize...@gmail.com> wrote:

Ouch, that's ugly. How about arrays? Is that supported? Like Service[]
services;?

On Aug 14, 12:17 pm, "Robbie Vanbrabant" < robbie.vanbrab...@gmail.com>
wrote:

Justin

unread,
Aug 14, 2007, 1:45:25 PM8/14/07
to google-guice
I'm attempting to add a lifecycle. I was just going to create an
interface called Service to has stop() and start(). Then iterate the
list to stop them all. I also wanted to have a controller that
iterates a list passing parameters to each Service.

On Aug 14, 12:37 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:


> That would get rid of the TypeLiteral stuff. But if you use the issue 123
> factory method it's basically the same.
> Depending on when or why you need the list of classes, you could perhaps
> also use AOP. What's the use case?
>
> Robbie
>

Justin

unread,
Aug 14, 2007, 1:47:39 PM8/14/07
to google-guice
I may just use Pico, as I'm wanting more the just DI. I was using it
before, but decided to give Guice a spin.

On Aug 14, 12:37 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:


> That would get rid of the TypeLiteral stuff. But if you use the issue 123
> factory method it's basically the same.
> Depending on when or why you need the list of classes, you could perhaps
> also use AOP. What's the use case?
>
> Robbie
>

Gregory Kick

unread,
Aug 14, 2007, 1:54:55 PM8/14/07
to google...@googlegroups.com
Could you accomplish this by injecting the controller into your
Service instances and having the services register themselves? It
seems like a design change or two might simplify things a lot.
Something like:

public interface Controller
{
public void registerService(Service service);
...
}

public class MyService implements Service
{
@Inject
public void init(Controller controller)
{
controller.registerService(this);
}
...
}

I just typed that up so it might be off a bit, but something like it
ought to accomplish what you're looking for.

On 8/14/07, Justin <rize...@gmail.com> wrote:
>


--
Gregory Kick
http://kickstyle.net/

Robbie Vanbrabant

unread,
Aug 14, 2007, 2:00:40 PM8/14/07
to google...@googlegroups.com
Is it perhaps this what you need?
Like, lifecycle callbacks for objects constructed by the container. At least if I understand that document correctly, I didn't read the whole thing.
 
On 8/14/07, Justin <rize...@gmail.com> wrote:

I may just use Pico, as I'm wanting more the just DI. I was using it
before, but decided to give Guice a spin.

On Aug 14, 12:37 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:
> That would get rid of the TypeLiteral stuff. But if you use the issue 123
> factory method it's basically the same.
> Depending on when or why you need the list of classes, you could perhaps
> also use AOP. What's the use case?
>
> Robbie
>
> On 8/14/07, Justin < rizen...@gmail.com> wrote:
>
>
>
> > Ouch, that's ugly. How about arrays? Is that supported? Like Service[]
> > services;?
>
> > On Aug 14, 12:17 pm, "Robbie Vanbrabant" < robbie.vanbrab...@gmail.com>
> > wrote:
> > > Here's how you can inject collections in 1.0:
>
> > > public class MultiValue {
> > >   public MultiValue(int i) {
> > >     System.out.println(i);
> > >   }
> > >   public static void main(String[] args) {
> > >     Injector i = Guice.createInjector (new Module() {

Justin

unread,
Aug 14, 2007, 2:05:35 PM8/14/07
to google-guice
Yes, that's what I'm using now. I was attempting to use Guice in place
of Pico.

On Aug 14, 1:00 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:
> Is it perhaps this what you need?http://www.picocontainer.org/lifecycle.html


> Like, lifecycle callbacks for objects constructed by the container. At least
> if I understand that document correctly, I didn't read the whole thing.
>

> On 8/14/07, Justin <rizen...@gmail.com> wrote:
>
>
>
> > I may just use Pico, as I'm wanting more the just DI. I was using it
> > before, but decided to give Guice a spin.
>
> > On Aug 14, 12:37 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
> > wrote:
> > > That would get rid of the TypeLiteral stuff. But if you use the issue
> > 123
> > > factory method it's basically the same.
> > > Depending on when or why you need the list of classes, you could perhaps
> > > also use AOP. What's the use case?
>
> > > Robbie
>
> > > On 8/14/07, Justin <rizen...@gmail.com> wrote:
>
> > > > Ouch, that's ugly. How about arrays? Is that supported? Like Service[]
> > > > services;?
>
> > > > On Aug 14, 12:17 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
> > > > wrote:
> > > > > Here's how you can inject collections in 1.0:
>
> > > > > public class MultiValue {
> > > > > public MultiValue(int i) {
> > > > > System.out.println(i);
> > > > > }
> > > > > public static void main(String[] args) {

> > > > > Injector i = Guice.createInjector(new Module() {

Robbie Vanbrabant

unread,
Aug 14, 2007, 2:08:33 PM8/14/07
to google...@googlegroups.com
In any case Guice currently doesn't support lifecycle callbacks. Gregory's suggestion is certainly worth considering.
 
Robbie
 

Justin

unread,
Aug 14, 2007, 2:10:32 PM8/14/07
to google-guice
Ok, thanks.

On Aug 14, 1:08 pm, "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
wrote:


> In any case Guice currently doesn't support lifecycle callbacks. Gregory's
> suggestion is certainly worth considering.
>
> Robbie
>

> On 8/14/07, Robbie Vanbrabant <robbie.vanbrab...@gmail.com> wrote:
>
>
>
> > Is it perhaps this what you need?
> >http://www.picocontainer.org/lifecycle.html
> > Like, lifecycle callbacks for objects constructed by the container. At
> > least if I understand that document correctly, I didn't read the whole
> > thing.
>

Robbie Vanbrabant

unread,
Aug 14, 2007, 2:17:22 PM8/14/07
to google...@googlegroups.com
Dhanji's quick comparison of Pico vs Guice:
Robbie
 
On 8/14/07, Justin <rize...@gmail.com> wrote:

Dhanji R. Prasanna

unread,
Aug 14, 2007, 7:44:44 PM8/14/07
to google...@googlegroups.com
If and when we get support for aopalliance ConstructorInterceptors, it ought to be easy to add in multicast and flexible pico-style lifecycle. I think pico rightfully distinguishes DI from lifecycle as two unrelated concepts. It just has a few multicast operations out of the box. There was a huge argument about this sometime ago:

http://code.google.com/p/google-guice/issues/detail?id=62

I think Hani was pushing strongly for "init" and (Bob might have agreed - duno? ), but Kevin and I were dead against any lifecycle support at all. In any case, multicast + Ctor interception solves everything elegantly, you can define your own lifecycle events and promulgate them how you choose.

My argument has always been lifecycle is deployment- (and often domain-) specific, but now with JEE profiles (JSR-316) it is conceivable that guice can be packaged as a JEE appserver/container in a limited profile (certainly have heard Rod Johnson say something along those lines about springframework). So this argument may become relevant again. Interesting stuff...

Dhanji.
Reply all
Reply to author
Forward
0 new messages