automatic dependency injection

31 views
Skip to first unread message

Luiz E. Kowaslki

unread,
Sep 21, 2011, 7:46:53 AM9/21/11
to google-guice
hi all
i'm pretty much new in guice, and now i have some questions.
i'm working on an RCP app (OSGi) and i'm using guice and peaberry
my activator is like this
Module mod = new AbstractModule() {

@Override
protected void configure() {
install(Peaberry.osgiModule(context));
}
};

injector = Guice.createInjector(mod, new GermantechModulo());

and this germantechmodulo


public class GermantechModulo extends AbstractModule {

@Override
protected void configure() {
bind(DAO.class).to(DAOImpl.class).in(Scopes.SINGLETON);
}

@Provides
public EntityManager getEntityManger(){
return ECFEntityManager.createEntityManager();
}

as you can see, in my 'configure()' I bind my dao (interface) to my
daoimpl...but i'll have a LOT of daos and repositories.
is there a way to tell guice to 'auto bind' these classes?
thanks a lot

Fred Faber

unread,
Sep 21, 2011, 8:31:05 AM9/21/11
to google...@googlegroups.com
In your case, how would Guice know what to bind?


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Luiz E. Kowalski

unread,
Sep 21, 2011, 10:11:44 AM9/21/11
to google...@googlegroups.com
i think i can use some annotation like @Repository or some other
convention, like spring's ClassPathScanningCandidateComponentProvider

Stuart McCulloch

unread,
Sep 21, 2011, 11:09:54 AM9/21/11
to google...@googlegroups.com
On 21 Sep 2011, at 10:11, Luiz E. Kowalski wrote:

> i think i can use some annotation like @Repository or some other
> convention, like spring's ClassPathScanningCandidateComponentProvider

There are a number of extensions / utilities that can scan annotations and bind implementations:

http://groups.google.com/group/google-guice/search?q=annotation+scanner&start=0&scoring=d&

and if you need special scanning it's not hard to write your own using something like http://code.google.com/p/reflections/ as a base - then it's just a matter of calling the Guice SPI just as before, except that this time you're looping over the results and calling "bind( clazz )..." rather than "bind( MyDAO.class )..."

HTH

Luiz E. Kowalski

unread,
Sep 21, 2011, 12:48:25 PM9/21/11
to google...@googlegroups.com
thanks. this will help me a lot!


cheers

Reply all
Reply to author
Forward
0 new messages