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