Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.tectonica.guice;
 
 
An optional injector for Mojarra-based applications where dependency injection is required into the @ManagedBeans. It extends com.sun.faces.vendor.WebContainerInjectionProvider, which normally handles invocations of @PostConstruct and @PreDestroy, by also adding dependency-injection for @ManagedBeans using the Guice injector created in GuiceListener. This creator, by the way, also handles @PostConstruct methods, so we make sure to avoid double invocation here.

To use, add the following paragraph to web.xml alongside your other JSF configuration:

 <context-param>
    <param-name>com.sun.faces.injectionProvider</param-name>
    <param-value>com.tectonica.guice.GuiceJsfInjector</param-value>
 </context-param>
 
NOTE: make sure your GuiceListener-subclass is an active listener in the web.xml, or NullPointerExceptions will be thrown

Author(s):
Zach Melamed
	public void inject(Object managedBeanthrows InjectionProviderException
	{
		if (isToBeInjectedByGuice(managedBean))
			GuiceListener.injectMembers(managedBean);
	}

as an arbitrary choice, the choice here is to inject only into @ManagedBean instances, so that other classes - not written by us - wouldn't be injected too. This choice could be altered.

Parameters:
managedBean
Returns:
	private boolean isToBeInjectedByGuice(Object managedBean)
	{
		return managedBean.getClass().getAnnotation(ManagedBean.class) != null;
	}
	public void invokePostConstruct(Object managedBeanthrows InjectionProviderException
	{
		// @PostConstruct is already handled in classes we injected
		if (!isToBeInjectedByGuice(managedBean))
			super.invokePostConstruct(managedBean);
	}
	public void invokePreDestroy(Object managedBeanthrows InjectionProviderException
	{
		super.invokePreDestroy(managedBean);
	}
New to GrepCode? Check out our FAQ X