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].
> 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.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
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.