Hi,

There are many ways to solve this problem, one easy enough to understand:

@Inject
Provider<CrawlerOne> crawlerOneProvider;

@Inject
Provider<CrawlerTwo> crawlerTwo;


public Crawler getCrawler(String url) {
    if (url...) {
        return crawlerOneProvider.get();
    }
    else {
        return crawlerTwoProvider.get();
    }
}


As an alternative you can inject the injector itself and get the instance
from it.

Also a slightly more advanced, but much more elgant way is to use
AssistedInject (it's exectly for this use case) See the guice docs for more
info.

PS: no need to make everything static, the whole point of the DI framework
that you don't need static anchors in your code.

--
L

--
L

On Sun, Mar 15, 2015 at 4:37 PM, Barak Yaish <[email protected]> wrote:

> Hi,
>
> Doing my first steps with Guice, I thought the fastest way to understand
> it would migrate parts of existing application to Guice style. The
> application is kind of web crawler, and I have factory creating crawlers
> based on the input url:
>
> public static Crawler getCrawler( String url )
> {
> try
> {
> if( url.contains( "www.site1.com" ) )
> return new Site1( AppConfig.getInstance() );
> else if( url.contains( "www.site2.com" ) )
> return new Site2( AppConfig.getInstance() );
> }
> catch ( Exception e )
> {
> logger.error( "failure", e );
> }
>
> return null;
> }
>
>
> Is the Guice version is only take the crawler instances from the injector
> and injecting the AppConfig?
>
> public static Crawler getCrawler( String url )
> {
> try
> {
> if( url.contains( "www.site1.com" ) )
> return GuiceInjector.getInstance( Site1.class );
> else if( url.contains( "www.site2.com" ) )
> return GuiceInjector.getInstance( Site2.class );
> }
> catch ( Exception e )
> {
> logger.error( "failure", e );
> }
>
> return null;
> }
>
> Is there a better and/or more elegant way?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-guice/8911e587-58bd-4631-80a2-7a5d91007431%40googlegroups.com
> <https://groups.google.com/d/msgid/google-guice/8911e587-58bd-4631-80a2-7a5d91007431%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/CAD-udUBRuBzSYL9-228W87Ab4fNSijVMSkeH7o0r6x4uQ3TbYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to