Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Guice initialize beans?

I've used Spring before (and like it), but thought I'd take a look at Guice.

Is there a way to initialize something like maps or lists into beans using Guice?

For instance, I've done the following before in Spring to inject a list of items I want to process into some bean.

<property name="FilesToProcess">
   <list>
      <value>file1.xml</value>
      <value>file2.xml</value>
   </list>
</property>

How can I do this in Guice?

like image 818
Vinnie Avatar asked Mar 01 '23 00:03

Vinnie


1 Answers

Guice2 has MultiBindings and MapBindings, which should work for you.

https://github.com/google/guice/wiki/Multibindings

Updated:

After looking at this again, it seems that you may be asking how you can inject runtime values into Guice, perhaps as arbitrary objects.

Guice is very focused around doing everything as typesafe code, so it doesn't lend itself naturally to this. What I've done to provide input to Guice is to create an XML schema and use jaxb to suck this in and inject the resulting objects.

There is a Names.bindProperties method for binding regular old properties into Guice constants.

There is also some level of integration with Spring, so you may want to look at this as well.

like image 83
Dave Stenglein Avatar answered Mar 13 '23 00:03

Dave Stenglein