Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.kasource.kaevent.bean;
 
 import java.util.Map;
 
 import  com.google.inject.Binding;
 import  com.google.inject.Inject;
 import  com.google.inject.Injector;
 import  com.google.inject.Key;
 import  com.google.inject.Singleton;
import  com.google.inject.name.Named;
import  com.google.inject.name.Names;

Bean Resolver for Guice. Will locate beans using the Guice Injector.

Author(s):
rikardwi
@Singleton
public class GuiceBeanResolver implements BeanResolver {
	@Inject
	private Injector injector;


Returns bean from Guice.

Parameters:
<T> Class of the bean
beanName Name of the bean to return.
ofType Class of the bean.
Returns:
Bean from the Guice Injector.
Throws:
CouldNotResolveBeanException if no bean named beanName can be found.
ClassCastException if the bean found could not be cast to T.
	@SuppressWarnings("unchecked")
    @Override
	public <T> T getBean(String beanNameClass<T> ofTypethrows CouldNotResolveBeanExceptionClassCastException {
		Key<T> bindKey = Key.get(ofType, Names.named(beanName));
		try {
			return  .getInstance(bindKey);
catch (RuntimeException e) {
			Key<?> beanKey = getBeanKey(beanName);
			if (beanKey == null) {
				throw new CouldNotResolveBeanException("Could not find any bean named " + beanName);
			}
			return (T) .getInstance(beanKey);
		}
	}

Returns the key for the bean with name beanName.

Parameters:
beanName Name of bean to find Key for.
Returns:
Key for named bean or null if no Key found.
	private Key<?> getBeanKey(String beanName) {
		Map<Key<?>, Binding<?>> bindings = .getBindings();
		for (Key<?> key : bindings.keySet()) {
			if (key.getAnnotationType() != null && key.getAnnotationType().equals(Named.class)) {
				Named named = (Named) key.getAnnotation();
				if (named.value().equals(beanName)) {
					return key;
				}
			}
		}
		return null;
	}
New to GrepCode? Check out our FAQ X