Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (C) 2014 Zach Melamed
  * Latest version available online at
  * 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
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * 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:

NOTE: make sure your GuiceListener-subclass is an active listener in the web.xml, or NullPointerExceptions will be thrown

Zach Melamed
	public void inject(Object managedBeanthrows InjectionProviderException
		if (isToBeInjectedByGuice(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.

	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))
	public void invokePreDestroy(Object managedBeanthrows InjectionProviderException
New to GrepCode? Check out our FAQ X