[jboss-dev-forums] [Design the new POJO MicroContainer] - Re: Microcontainer + Guice

alesj do-not-reply at jboss.com
Wed Sep 26 06:19:17 EDT 2007


I've also added a way to do injection the other way around: from Guice into MC.


  |          AbstractBeanMetaData guicePlugin = new AbstractBeanMetaData("GuicePlugin", GuiceKernelRegistryEntryPlugin.class.getName());
  |          AbstractConstructorMetaData constructor = new AbstractConstructorMetaData();
  |          AbstractArrayMetaData arrayMetaData = new AbstractArrayMetaData();
  |          final Singleton singleton = new Singleton();
  |          Module module = new AbstractModule()
  |          {
  |             protected void configure()
  |             {
  |                bind(Singleton.class).toInstance(singleton);
  |             }
  |          };
  |          arrayMetaData.add(new AbstractValueMetaData(module));
  |          constructor.setParameters(Collections.singletonList((ParameterMetaData)new AbstractParameterMetaData(arrayMetaData)));
  |          guicePlugin.setConstructor(constructor);
  |          controller.install(guicePlugin);
  | 
  |          BeanMetaData holderBean = new AbstractBeanMetaData("holder", SingletonHolder.class.getName());
  |          controller.install(holderBean);
  | 
  |          ControllerContext holderContext = controller.getInstalledContext("holder");
  |          assertNotNull(holderContext);
  |          SingletonHolder holder = (SingletonHolder)holderContext.getTarget();
  |          assertNotNull(holder);
  |          assertEquals(singleton, holder.getSingleton());
  | 
  | 

The detail is in GuiceKernelRegistryEntryPlugin, which acts as a middle man between MC registry and Guice Injector instance.


  | public class GuiceKernelRegistryEntryPlugin implements KernelRegistryPlugin
  | {
  |    private Injector injector;
  | 
  |    public GuiceKernelRegistryEntryPlugin(Module... modules)
  |    {
  |       injector = Guice.createInjector(modules);
  |    }
  | 
  |    public void destroy()
  |    {
  |       injector = null;
  |    }
  | 
  |    public KernelRegistryEntry getEntry(Object name)
  |    {
  |       KernelRegistryEntry entry = null;
  |       try
  |       {
  |          if (name instanceof Class<?>)
  |          {
  |             Class<?> clazz = (Class<?>)name;
  |             entry = new AbstractKernelRegistryEntry(name, injector.getInstance(clazz));
  |          }
  |          else if (name instanceof Key)
  |          {
  |             Key<?> key = (Key<?>)name;
  |             entry = new AbstractKernelRegistryEntry(name, injector.getInstance(key));
  |          }
  |       }
  |       catch (Exception ignored)
  |       {
  |       }
  |       return entry;
  |    }
  | }
  | 

MC documentation has already been updated with this new Guice-int module.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4088760#4088760

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4088760



More information about the jboss-dev-forums mailing list