Great! yes that helps. I didn't realize the previous statement wasn't
actually binding as well.
Trying to make this a CheckedProvider now as it may throw exceptions. I'm
not able to pass in config to the provider instance when using this
method.
I know i would implements CheckedProvider<KeyedObjectPool>, but the bind
isn't happy.
In general what is best practice for passing in parameters to the
provider? I've also looked at assisted injection, but not sure which route
to go.
v/r - Sonny
On Monday, August 27, 2012 1:04:12 PM UTC-7, Jared Bunting wrote:
>
> You never bound a KeyedObjectPool, you just bound KeyedObjectPool
> annotated with @Named("pool")
>
> If you change the last line of your main method, it will work:
>
> Guice.createInjector(new Mod(config))
> .getInstance(Key.get(KeyedObjectPool.class,
> Names.named("pool")));
>
> Another option is to add the binding:
>
> bind(KeyedObjectPool.class).to(Key.get(KeyedObjectPool.class,
> Names.named("pool")));
>
>
> Hope that helps,
> Jared
>
> On Mon 27 Aug 2012 02:47:52 PM CDT, Sonny Heer wrote:
> >
> >
> >
> > New to Guice.
> >
> > I'm trying to inject the apache commons pool using Guice as a
> > singleton for use in other parts of the application/modules. When i
> > use a simple Foo pool Object, it works fine. But using the actual
> > KeyObjectPool causes:
> > 1) No implementation for org.apache.commons.pool.KeyedObjectPool was
> > bound.
> > while locating org.apache.commons.pool.KeyedObjectPool
> >
> >
> > Below is the code, so it can be run. Obviously I'm missing something
> > fundamental/obvious here. Any help would be appreciated. Thanks -
> Sonny
> >
> > class Foo {
> > public Foo() {
> > }
> > }
> >
> > ===================================================
> >
> > import java.io.Serializable;
> >
> > import org.apache.commons.pool.KeyedObjectPool;
> > import org.apache.commons.pool.impl.GenericKeyedObjectPool;
> >
> > import com.google.inject.AbstractModule;
> > import com.google.inject.Guice;
> > import com.google.inject.Inject;
> > import com.google.inject.Provider;
> > import com.google.inject.name.Named;
> > import com.google.inject.name.Names;
> >
> > public class PoolInjectMain {
> >
> > public static void main(String[] args) {
> > Config config = new Config();
> > config.setInstanceName("inst");
> > config.setZookeepers("192.168.1.1:2181");
> > config.setUsername("user");
> > config.setPassword("pass");
> > config.setPoolBatchThreadCount(1);
> >
> > Guice.createInjector(new Mod(config))
> > .getInstance(KeyedObjectPool.class);
> > }
> > }
> >
> > class KeyedObjectPoolProvider implements Provider<KeyedObjectPool> {
> > private Config config;
> >
> > public KeyedObjectPoolProvider(Config config) {
> > System.out.println("KeyedObjectPoolProvider created with: " +
> > config);
> > this.config = config;
> > }
> >
> > @Override
> > public KeyedObjectPool get() {
> > // USE config here, create pool.
> > GenericKeyedObjectPool pool = new GenericKeyedObjectPool(null);
> > // LOG.info("GenericKeyedObjectPool created in factory");
> > return pool;
> > }
> > }
> >
> > class Mod extends AbstractModule {
> > private Config config;
> >
> > public Mod(Config config) {
> > System.out.println("Mod created with: " + config);
> > this.config = config;
> > }
> >
> > @Override
> > protected void configure() {
> > bind(KeyedObjectPool.class).annotatedWith(Names.named("pool"))
> > .toProvider(new KeyedObjectPoolProvider(config));
> > }
> > }
> >
> > class InjectPool {
> > @Inject
> > public void setPool(@Named("pool") KeyedObjectPool pool) {
> > System.out.println("injected pool: " + pool);
> > }
> > }
> >
> > class Config implements Serializable {
> >
> > private static final long serialVersionUID = 1L;
> > private String username;
> > private String password;
> > private String instanceName;
> > private String zookeepers;
> > private int poolBatchThreadCount;
> >
> > public String getUsername() {
> > return username;
> > }
> >
> > public void setUsername(String username) {
> > this.username = username;
> > }
> >
> > public String getPassword() {
> > return password;
> > }
> >
> > public void setPassword(String password) {
> > this.password = password;
> > }
> >
> > public String getInstanceName() {
> > return instanceName;
> > }
> >
> > public void setInstanceName(String instanceName) {
> > this.instanceName = instanceName;
> > }
> >
> > public String getZookeepers() {
> > return zookeepers;
> > }
> >
> > public void setZookeepers(String zookeepers) {
> > this.zookeepers = zookeepers;
> > }
> >
> > public int getPoolBatchThreadCount() {
> > return poolBatchThreadCount;
> > }
> >
> > public void setPoolBatchThreadCount(int poolBatchThreadCount) {
> > this.poolBatchThreadCount = poolBatchThreadCount;
> > }
> >
> > }
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "google-guice" group.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msg/google-guice/-/gVu_ZLTlb0UJ.
> > To post to this group, send email to
> > [email protected]<javascript:>.
>
> > To unsubscribe from this group, send email to
> > [email protected] <javascript:>.
> > For more options, visit this group at
> > http://groups.google.com/group/google-guice?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/s8v9mDx_hbkJ.
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.