本文整理汇总了Java中javax.enterprise.inject.spi.BeanManager.createCreationalContext方法的典型用法代码示例。如果您正苦于以下问题:Java BeanManager.createCreationalContext方法的具体用法?Java BeanManager.createCreationalContext怎么用?Java BeanManager.createCreationalContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.enterprise.inject.spi.BeanManager
的用法示例。
在下文中一共展示了BeanManager.createCreationalContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testInjectionTarget
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@Test
public void testInjectionTarget() {
BeanManager beanManager = current().getBeanManager();
// CDI uses an AnnotatedType object to read the annotations of a class
AnnotatedType<String> type = beanManager.createAnnotatedType(String.class);
// The extension uses an InjectionTarget to delegate instantiation,
// dependency injection
// and lifecycle callbacks to the CDI container
InjectionTarget<String> it = beanManager.createInjectionTarget(type);
// each instance needs its own CDI CreationalContext
CreationalContext<String> ctx = beanManager.createCreationalContext(null);
// instantiate the framework component and inject its dependencies
String instance = it.produce(ctx); // call the constructor
it.inject(instance, ctx); // call initializer methods and perform field
// injection
it.postConstruct(instance); // call the @PostConstruct method
// destroy the framework component instance and clean up dependent
// objects
assertNotNull("the String instance is injected now", instance);
assertTrue("the String instance is injected now but it's empty", instance.isEmpty());
it.preDestroy(instance); // call the @PreDestroy method
it.dispose(instance); // it is now safe to discard the instance
ctx.release(); // clean up dependent objects
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:25,代码来源:InjectSPITestCase.java
示例2: getInstancesByType
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> List<T> getInstancesByType(Class<T> clazz) {
BeanManager beanManager = getBeanManager();
Type type = clazz;
if (clazz == JsonApiExceptionMapper.class) {
TypeLiteral<JsonApiExceptionMapper<?>> typeLiteral = new TypeLiteral<JsonApiExceptionMapper<?>>() {
};
type = typeLiteral.getType();
}
Set<Bean<?>> beans = beanManager.getBeans(type);
List<T> list = new ArrayList<>();
for (Bean<?> bean : beans) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
T object = (T) beanManager.getReference(bean, type, creationalContext);
list.add(object);
}
return list;
}
示例3: getInstancesByAnnotation
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@Override
public <A extends Annotation> List<Object> getInstancesByAnnotation(Class<A> annotationClass) {
BeanManager beanManager = getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(Object.class);
List<Object> list = new ArrayList<>();
for (Bean<?> bean : beans) {
Class<?> beanClass = bean.getBeanClass();
Optional<A> annotation = ClassUtils.getAnnotation(beanClass, annotationClass);
if (annotation.isPresent()) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
Object object = beanManager.getReference(bean, beanClass, creationalContext);
list.add(object);
}
}
return list;
}
示例4: getBean
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
BeanManager bm = getBeanManager();
Set<Bean<?>> beans = bm.getBeans(clazz);
if (beans == null || beans.isEmpty()) {
return null;
}
Bean<T> bean = (Bean<T>) beans.iterator().next();
CreationalContext<T> ctx = bm.createCreationalContext(bean);
T o = (T) bm.getReference(bean, clazz, ctx);
return o;
}
示例5: getInstancesByAnnotation
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@Override
public <A extends Annotation> List<Object> getInstancesByAnnotation(Class<A> annotationClass) {
BeanManager beanManager = CDI.current().getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(Object.class);
List<Object> list = new ArrayList<>();
for (Bean<?> bean : beans) {
Class<?> beanClass = bean.getBeanClass();
Optional<A> annotation = ClassUtils.getAnnotation(beanClass, annotationClass);
if (annotation.isPresent()) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
Object object = beanManager.getReference(bean, beanClass, creationalContext);
list.add(object);
}
}
return list;
}
示例6: inject
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
void inject() {
BeanManager beanManager = container.getBeanManager();
CreationalContext<Object> ctx = beanManager.createCreationalContext(null);
@SuppressWarnings("unchecked")
InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) beanManager
.getInjectionTargetFactory(beanManager.createAnnotatedType(instance.getClass())).createInjectionTarget(null);
injectionTarget.inject(instance, ctx);
creationalContext = ctx;
}
示例7: locate
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@Override
public JsonDriver locate(Annotation[] fieldAnnotations, Optional<String> name) {
final BeanManager bm = CDI.current().getBeanManager();
final Annotation[] qualifiers = Arrays.stream(fieldAnnotations).filter(a -> searchAnnotation(a, Qualifier.class).isPresent()).toArray(n -> new Annotation[n]);
JsonDriverNotFound.failIf(qualifiers.length > 0 && name.isPresent(), "found both @Qualifiers and @WithDriver.value()");
final Set<Bean<?>> beans = name.map(n -> bm.getBeans(n)).orElseGet(() -> bm.getBeans(JsonDriver.class, qualifiers));
JsonDriverNotFound.failIf(beans.isEmpty(), "no JsonDriver found in BeanManager");
JsonDriverNotFound.failIf(beans.size() > 1, "more than one JsonDriver found in BeanManager, use @JsonType.WithDriver to disambiguate");
final Bean<?> bean = beans.iterator().next();
final CreationalContext<?> ctx = bm.createCreationalContext(bean);
return (JsonDriver) bm.getReference(bean, JsonDriver.class, ctx);
}
示例8: getInstancesByType
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> List<T> getInstancesByType(Class<T> clazz) {
BeanManager beanManager = CDI.current().getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(clazz);
List<T> list = new ArrayList<>();
for (Bean<?> bean : beans) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
T object = (T) beanManager.getReference(bean, clazz, creationalContext);
list.add(object);
}
return list;
}
示例9: HandlerInstance
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
HandlerInstance(AnnotatedType<T> annotatedType, BeanManager beanManager) {
this.annotatedType = annotatedType;
this.injectionTarget = beanManager.getInjectionTargetFactory(annotatedType).createInjectionTarget(null);
this.creationalContext = beanManager.createCreationalContext(null);
this.instance = injectionTarget.produce(creationalContext);
injectionTarget.inject(instance, creationalContext);
injectionTarget.postConstruct(instance);
}
示例10: inject
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
/**
* Perform injection into a non-CDI managed bean.
* <p>
* An example of this is a JSP Tag
* <p>
* @param <T>
* @param bean
*/
public static <T> T inject( T bean )
{
BeanManager beanManager = getBeanManager();
AnnotatedType<T> annotatedType = beanManager.<T>createAnnotatedType( (Class<T>) bean.getClass() );
InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget( annotatedType );
CreationalContext ctx = beanManager.createCreationalContext( null );
injectionTarget.inject( bean, ctx );
return bean;
}
示例11: get
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
protected CamelContext get(String name) {
BeanManager beanManager = BeanManagerLookup.getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(name);
if(beans.isEmpty())
return null;
@SuppressWarnings("unchecked")
Bean<CamelContext> bean = (Bean<CamelContext>) beans.iterator().next();
CreationalContext<CamelContext> ctx = beanManager.createCreationalContext(bean);
return (CamelContext) beanManager.getReference(bean, CamelContext.class, ctx);
}
示例12: lookup
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T lookup(Class<T> clazz, BeanManager bm) {
Iterator<Bean<?>> iter = bm.getBeans(clazz).iterator();
if (!iter.hasNext()) {
throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type " + clazz.getName());
}
Bean<T> bean = (Bean<T>) iter.next();
CreationalContext<T> ctx = bm.createCreationalContext(bean);
T dao = (T) bm.getReference(bean, clazz, ctx);
return dao;
}
示例13: onStartup
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onStartup(@Observes @Initialized WebApplication webApplication, BeanManager beanManager)
{
for( Bean bean : beanManager.getBeans(Startup.class) )
{
CreationalContext context = beanManager.createCreationalContext(bean);
Startup st = (Startup) beanManager.getReference(bean, Startup.class, context);
st.init();
}
}
示例14: newBean
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
/**
* Create a new CDI bean given its class and a bean manager. The bean is created
* in the context defined by the scope annotation on the class.
*
* @param bm The BeanManager.
* @param clazz CDI class.
* @param <T> class parameter.
* @return newly allocated CDI bean.
*/
public static <T> T newBean(BeanManager bm, Class<T> clazz) {
Set<Bean<?>> beans = bm.getBeans(clazz);
final Bean<T> bean = (Bean<T>) bm.resolve(beans);
final CreationalContext<T> ctx = bm.createCreationalContext(bean);
return (T) bm.getReference(bean, clazz, ctx);
}
示例15: getInstance
import javax.enterprise.inject.spi.BeanManager; //导入方法依赖的package包/类
/**
* Get the instance of a bean
*
* @param <T> type of bean
* @param bean Bean
* @param clazz class of bean
*
* @return
*/
public static <T> T getInstance( Bean<T> bean, Class<T> clazz )
{
BeanManager beanManager = getBeanManager();
CreationalContext<T> ctx = beanManager.createCreationalContext( bean );
return (T) beanManager.getReference( bean, clazz, ctx );
}