Este contenido no está disponible en el idioma seleccionado.

6.4. Using Guice With the Microcontainer


The focus of Guice is type matching. Guice beans are generated and configured using Modules.

Example 6.4. Deployment Descriptor for Guice Integration In the Microcontainer

<deployment xmlns="urn:jboss:bean-deployer:2.0">

  <bean name="GuicePlugin" class="org.jboss.guice.spi.GuiceKernelRegistryEntryPlugin">
    <constructor>
      <parameter>
	<array elementClass="com.google.inject.Module">
	  <bean class="org.jboss.demos.models.guice.PojoModule"/>
	</array>
      </parameter>
    </constructor>
  </bean>

</deployment>
			
			
			

Copy to Clipboard Toggle word wrap
Two important parts to watch from this file are PojoModule and GuiceKernelRegistryEntryPlugin. PojoModule configures your beans, as in Example 6.5, “Configuring Beans for Guice”. GuiceKernelRegistryEntryPlugin provides integration with the Microcontainer, as shown in Example 6.6, “Guice Integration with the Microcontainer”.

Example 6.5. Configuring Beans for Guice

public class PojoModule extends AbstractModule {
    private Controller controller;

    @Constructor
	public PojoModule(@Inject(
				  bean = KernelConstants.KERNEL_CONTROLLER_NAME) 
			  Controller controller)
    {
	this.controller = controller;
    }

    protected void configure()
    {
	bind(Controller.class).toInstance(controller);
	bind(IPojo.class).to(Pojo.class).in(Scopes.SINGLETON);
	bind(IPojo.class).annotatedWith(FromMC.class).
	    toProvider(GuiceIntegration.fromMicrocontainer(IPojo.class, "PlainPojo"));
    }
}
			
			
			

Copy to Clipboard Toggle word wrap

Example 6.6. Guice Integration with the Microcontainer

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;
    }	
}
			
			
			

Copy to Clipboard Toggle word wrap

Note

An Injector is created from the Modules class, then does a look-up on it for matching beans. See Section 6.5, “Legacy MBeans, and Mixing Different Component Models” for information about declaring and using legacy MBeans.
Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat