org.jboss.narayana.compensations.impl.BeanManagerUtil Maven / Gradle / Ivy
package org.jboss.narayana.compensations.impl;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import java.util.Iterator;
public class BeanManagerUtil {
public static BeanManager getBeanManager() {
return CDI.current().getBeanManager();
}
public static T createBeanInstance(Class clazz, BeanManager bm) {
Iterator> iter = bm.getBeans(clazz).iterator();
if (!iter.hasNext()) {
throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type " + clazz.getName());
}
Bean bean = (Bean) iter.next();
CreationalContext ctx = bm.createCreationalContext(bean);
return (T) bm.getReference(bean, clazz, ctx);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy