从BeanManager获取Bean
/* * Copyright 2013 GiavaCms.org. * * Licensed under the Eclipse Public License version 1.0, available at * http://www.eclipse.org/legal/epl-v10.html */ import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.Bean;//来自 N o w J a v a . c o m - 时代Java import javax.enterprise.inject.spi.BeanManager; import javax.naming.Context; import javax.naming.InitialContext; public class Main{ public static <T> T getBean(Class<T> beanClass) { try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/"); BeanManager beanManager = (BeanManager) envCtx .lookup("BeanManager"); Bean phBean = (Bean) beanManager.getBeans(beanClass).iterator() .next(); CreationalContext cc = beanManager/**来自 时代Java公众号**/ .createCreationalContext(phBean); T bean = (T) beanManager.get