On 27 March 2010 21:11, yarco <[email protected]> wrote:

> Guice is not injecting objects for WordDAOImpl. Im not sure if I'm
> doing anything right.
>
> In Spring I would usually create two named @Bean methods that expose
> two different objects of the same class.
> I'm not sure how it it properly done here
>
>
>  bind(JDBCQueryExecutor.class).to(JDBCQueryExecutorImpl.class);
>
>        Multibinder<WordProcessor> multibinder = Multibinder
>                                                .newSetBinder(binder(),
> WordProcessor.class, Names
>
>  .named("wordProcessors"));
>
>                                for (final String tableName : ...) {
>
>
>  multibinder.addBinding().toProvider(new Provider<WordProcessor>()
> {
>
>                                                public WordProcessor get() {
>                                                        return new
> DBCommonWordRemover(new WordDAOImpl(tableName));
>

^ you're manually creating WordDAOImpl here using 'new' and manually
injecting it into DBCommonWordRemover,
Guice doesn't know anything about this manual creation which is why the
instance doesn't get injected by Guice.

if you want your WordDAOImpl instance injected you have a couple of options:

1)  let Guice create and inject it for you (you'd need the AssistedInject
extension to customize tableName)
     http://code.google.com/p/google-guice/wiki/AssistedInject

2)  ask Guice to inject the instance during the injection cycle by calling
"binder.requestInjection( instance );"

HTH

                                               };
>                                        });
>
> public class WordDAOImpl implements WordDAO {
>        private String dbName;
>        public WordDAOImpl(String dbName) {
>                this.dbName = dbName;
>        }
>        @Inject
>        private JDBCQueryExecutor queryExecutor;   <<<<- Nothing is injected
> here
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>


-- 
Cheers, Stuart

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

Reply via email to