All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.narayana.compensations.impl.BeanManagerUtil Maven / Gradle / Ivy

There is a newer version: 7.1.0.Final
Show newest version
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