本文整理汇总了Java中javax.enterprise.inject.spi.InjectionTarget.produce方法的典型用法代码示例。如果您正苦于以下问题:Java InjectionTarget.produce方法的具体用法?Java InjectionTarget.produce怎么用?Java InjectionTarget.produce使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.enterprise.inject.spi.InjectionTarget
的用法示例。
在下文中一共展示了InjectionTarget.produce方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testInjectionTarget
import javax.enterprise.inject.spi.InjectionTarget; //导入方法依赖的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: createProducer
import javax.enterprise.inject.spi.InjectionTarget; //导入方法依赖的package包/类
@Override
public Producer createProducer() throws IllegalAccessException {
// FIXME: to be replaced once event firing with dynamic parameterized type
// is properly supported (see https://issues.jboss.org/browse/CDI-516)
TypeLiteral<T> literal = new TypeLiteral<T>() {
};
for (Field field : TypeLiteral.class.getDeclaredFields()) {
if (field.getType().equals(Type.class)) {
field.setAccessible(true);
field.set(literal, type);
break;
}
}
InjectionTarget<AnyEvent> target = manager.createInjectionTarget(manager.createAnnotatedType(AnyEvent.class));
CreationalContext<AnyEvent> ctx = manager.createCreationalContext(null);
AnyEvent instance = target.produce(ctx);
target.inject(instance, ctx);
return new CdiEventProducer<>(this, instance.event
.select(literal, qualifiers.stream().toArray(Annotation[]::new)));
}
示例3: newInstance
import javax.enterprise.inject.spi.InjectionTarget; //导入方法依赖的package包/类
private static <T> T newInstance(final OpenEjbConfig config, final Class<T> clazz) throws Exception {
final WebBeansContext webBeansContext = AppFinder.findAppContextOrWeb(
Thread.currentThread().getContextClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE);
if (webBeansContext == null) {
return clazz.newInstance();
}
final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
if (!beanManager.isInUse()) {
return clazz.newInstance();
}
final AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
final InjectionTarget<T> it = beanManager.createInjectionTarget(annotatedType);
final CreationalContext<T> context = beanManager.createCreationalContext(null);
final T instance = it.produce(context);
it.inject(instance, context);
it.postConstruct(instance);
config.releasables.add(new Releasable<T>(context, it, instance));
return instance;
}