Hello,

You should use the following Provider:

public class TemplateResolverProvider implements Provider<TemplateResolver>
{

    @Inject Injector injector;

    @Override
    public TemplateResolver get() {
        String templateResolverClass = .......; /* some logic */
        // FIXME : Wrong. Use the DI
        Class<? extends TemplateResolver> templateResolver =
AppClassloader.classloader().loadClass(templateResolverClass).asSubclass(TemplateResolver.class);
        TemplateResolver instance =
templateResolver.getDeclaredConstructor(TemplateRendererConfig.class).newInstance(templateRendererConfig);
        injector.injectMembers(instance);
        return instance;
    }
}

I hope this helps.


Regards,
Olivier

2015-01-29 12:38 GMT+01:00 Stephan Classen <[email protected]>:

>  Hi
>
> Will this configurable provider return the same concrete type of object
> during the whole live time of a running program?
> Or will the returned type change during runtime?
>
> Because if all you want to achieve is to be able to have mocks during unit
> test and real implementations during runtime then there are other ways to
> do this.
> There are also possibilities to change the bindings at the time the
> injector is constructed if you would need different types depending on some
> environment.
>
> The really tricky part is when you need to change the returned type during
> runtime.
>
>
>
>
> On 01/29/2015 08:10 AM, Igmar Palsenberg wrote:
>
> Hi,
>
>  I'm using a Provider to dynamically provide a certain class. The
> short(ed) code :
>
>
>  public class TemplateConfigurationModule extends AbstractModule {
>
>      void configure() {
>               bind(TemplateResolver.class).toProvider(
> TemplateResolverProvider.class).in(Singleton.class);
>      }
> }
>
>  public class TemplateResolverProvider implements
> Provider<TemplateResolver> {
>  @Override
>  public TemplateResolver get() {
>  String templateResolverClass = .......; /* some logic */
>                 // FIXME : Wrong. Use the DI
>  Class<? extends TemplateResolver> templateResolver = AppClassloader.
> classloader().loadClass(templateResolverClass).
> asSubclass(TemplateResolver.class);
>              return templateResolver.getDeclaredConstructor(
> TemplateRendererConfig.class).newInstance(templateRendererConfig);
>  }
> }
>
>  public interface TemplateResolver() {
>      .. Some stuff
> }
>
>
>  public class MockTemplateResolver implements TemplateResolver {
>       @Inject
>       SomeObject obj;
> }
>
>  So, the TemplateResolverProvider resolved a TemplateResolver class,
> which then at the end wants to provide a MockTemplateResolver. I've tried
> two things :
>
>  1) Use the normal classloader : In that case, no @Inject gets
> fullfilled. Obviously.
> 2) Use Guice to give me a TemplateResolver class
>
>  The latter also fails : I want it to construct the object and do the
> injections, but it uses the Provider. Also logica.
> Then I'm kinda lost : Is there a way to solve this ? For now, I've move
> the @Inject of SomeObject to the Provider, but that doesn't feel right.
>
>  Any advise ?
>
>
>  Regards,
>
>
>  Igmar
> --
> 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/6687a71a-5a3d-4e6c-87ed-170659d5c2d4%40googlegroups.com
> <https://groups.google.com/d/msgid/google-guice/6687a71a-5a3d-4e6c-87ed-170659d5c2d4%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/54CA1BC4.3000505%40gmx.ch
> <https://groups.google.com/d/msgid/google-guice/54CA1BC4.3000505%40gmx.ch?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Olivier Grégoire

-- 
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/CAORw%3DcMtiw63JHjJFUxQ%3DfJCuVAujMhvFz%3DMs1ayR1oZYgrj2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to