Object o = bm.getReference(bean, bean.getBeanClass(), ctx);
return o;
}
public static <T> T getBeanByType(Class<T> type) {
BeanManager bm = getBeanManager();
Set<Bean<?>> beans = bm.getBeans(type);
if (beans.isEmpty()) throw new IllegalArgumentException("Bean not found by type: " + type.getName());
// Get the bean that matches exactly the given class.
for (Bean<?> bean : beans) {
if (bean.getBeanClass().equals(type)) {
CreationalContext ctx = bm.createCreationalContext(bean);
Object o = bm.getReference(bean, bean.getBeanClass(), ctx);
return type.cast(o);
}
}
// Get the first bean found that implements the given type.
Bean bean = beans.iterator().next();
CreationalContext ctx = bm.createCreationalContext(bean);
Object o = bm.getReference(bean, bean.getBeanClass(), ctx);
return type.cast(o);
}