Este conteúdo não está disponível no idioma selecionado.

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>
			
			
			

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

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

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.
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2026 Red Hat
Voltar ao topo