java - Configuring a Provider that returns a generic type in Guice -



java - Configuring a Provider that returns a generic type in Guice -

i'm trying set provider daos created using jdbi. jdbi uses handle object (which wrapper around jdbc connection) , can hold of dao using handle.attach(mydaotype.class). rather having write separate provider implementation every dao class thought create sense this:

public class daoprovider<t> implements provider<t> { private final class<t> daotype; private final handle handle; @injected public daoprovider(class<t> daotype, handle handle) { this.daotype = daotype; this.handle = handle; } @override public t get() { homecoming handle.attach(daotype); } }

but seems tremendously hard wire guice. have tried using @assisted annotation on 1st constructor argument suggested in this answer. defined mill this:

public interface daoproviderfactory { <t> daoprovider<t> create(class<t> daotype); }

but it's not clear how should invoke factorymodulebuilder.implemented method whole point don't want have extend provider class.

it seems bit crazy i'd have factory returns provider returns thing want!

it strikes me easy spring di container want believe it's possible guice. can point me in right direction?

thanks @condit pointing me @ enabled me solve issue. it's simple. changed provider implementation utilize field injection handler this:

public class daoprovider<t> implements provider<t> { private @inject handle handle; private final class<t> daotype; public daoprovider(class<t> daotype) { this.daotype = daotype; } @override public t get() { homecoming handle.attach(daotype); } }

then in module or application have specific dao classes want bind can this:

bind(userstore.class).toprovider(new daoprovider<>(userstore.class)); bind(messagestore.class).toprovider(new daoprovider<>(messagestore.class));

guice injects handle daoprovider instances automatically.

java dependency-injection guice jdbi

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -