Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (C) 2014 Zach Melamed
  * 
  * Latest version available online at https://github.com/zach-m/tectonica-commons
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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