本文整理汇总了Java中javax.enterprise.inject.spi.InjectionTarget类的典型用法代码示例。如果您正苦于以下问题:Java InjectionTarget类的具体用法?Java InjectionTarget怎么用?Java InjectionTarget使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InjectionTarget类属于javax.enterprise.inject.spi包,在下文中一共展示了InjectionTarget类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: createAndDestroyFSShouldRegisterUnregisterOnPriorityDisposableRegistry
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
@Test
public void createAndDestroyFSShouldRegisterUnregisterOnPriorityDisposableRegistry() throws Exception {
when(bm.getBeans("configIO")).thenReturn(configIOBeans);
when(bm.getReference(eq(ioServiceBean),
eq(IOService.class),
any(CreationalContext.class)))
.thenReturn(ioServiceMock);
when(ioServiceMock.newFileSystem(any(URI.class),
any(Map.class)))
.thenReturn(fs);
final Bean fileSystemBean = producer.createFileSystemBean(bm,
mock(InjectionTarget.class));
assertNull(PriorityDisposableRegistry.get("systemFS"));
fileSystemBean.create(mock(CreationalContext.class));
assertNotNull(PriorityDisposableRegistry.get("systemFS"));
fileSystemBean.destroy(fs,
mock(CreationalContext.class));
assertNull(PriorityDisposableRegistry.get("systemFS"));
}
示例4: initializePropertyLoading
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
/**
* Prepares the injection process of properties from a property file.
*
* @param pit
* The actual target of process injection
* @param <T>
* the generic type of the injection target
*/
public <T> void initializePropertyLoading(@Observes final ProcessInjectionTarget<T> pit) throws IOException {
AnnotatedType<T> at = pit.getAnnotatedType();
if (!at.isAnnotationPresent(PropertyFile.class)) {
return;
}
try {
PropertyFile propertyFile = at.getAnnotation(PropertyFile.class);
Properties properties = loadProperties(propertyFile, pit.getAnnotatedType().getJavaClass());
Map<Field, Object> fieldValues = assignPropertiesToFields(at.getFields(), properties);
InjectionTarget<T> wrapped = new PropertyInjectionTarget<T>(fieldValues, pit, pit.getInjectionTarget());
pit.setInjectionTarget(wrapped);
} catch (Exception e) {
pit.addDefinitionError(e);
}
}
示例5: createInjectionTarget
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private static <T> InjectionTarget<T> createInjectionTarget(
final CompletableFuture<Registry> injector, final Class<T> type) {
return Reflection.newProxy(InjectionTarget.class, (proxy, method, args) -> {
final String name = method.getName();
switch (name) {
case "produce":
return injector.get().require(type);
case "inject":
return null;
case "postConstruct":
return null;
case "preDestroy":
return null;
case "dispose":
return null;
default:
throw new UnsupportedOperationException(method.toString());
}
});
}
示例6: 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;
}
示例7: createInjectionTarget
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
@Override
public InjectionTarget<T> createInjectionTarget(final Bean<T> bean) {
final EjbInjectionTargetImpl<T> injectionTarget = new EjbInjectionTargetImpl<T>(getAnnotatedType(), createInjectionPoints(bean), getWebBeansContext());
final InjectionTarget<T> it = getWebBeansContext().getWebBeansUtil().fireProcessInjectionTargetEvent(injectionTarget, getAnnotatedType()).getInjectionTarget();
for (final InjectionPoint ip : it.getInjectionPoints()) {
if (ip.getType() != UserTransaction.class) {
continue;
}
if (beanContext.getTransactionType() != TransactionType.BeanManaged) {
throw new DefinitionException("@Inject UserTransaction is only valid for BeanManaged beans");
}
}
if (!EjbInjectionTargetImpl.class.isInstance(it)) {
return new EjbInjectionTargetImpl<T>(injectionTarget, it);
}
return it;
}
示例8: programmaticInjection
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
/**
* Manually inject dependencies. Inspired by https://developer.jboss.org/thread/196807
*
* @param clazz
* @param injectionObject
* @throws NamingException
*/
public static <T> T programmaticInjection(Class<T> clazz, T injectionObject) throws NamingException {
InitialContext initialContext = new InitialContext();
Object lookup = initialContext.lookup("java:comp/BeanManager");
BeanManager beanManager = (BeanManager) lookup;
AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
injectionTarget.inject(injectionObject, creationalContext);
injectionTarget.postConstruct(injectionObject); //call the @PostConstruct method
creationalContext.release();
return injectionObject;
}
示例9: setUp
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
@Before
public final void setUp() throws Exception {
System.out.printf("AbstractCdiContainerTest#setUp() containerRefCount=%d, cdiContainer=%s\n", containerRefCount.get(), cdiContainer );
if ( cdiContainer != null ) {
containerRefCount.incrementAndGet();
final ContextControl ctxCtrl = BeanProvider.getContextualReference(ContextControl.class);
//stop the RequestContext to dispose of the @RequestScoped EntityManager
ctxCtrl.stopContext(RequestScoped.class);
//immediately restart the context again
ctxCtrl.startContext(RequestScoped.class);
// perform injection into the very own test class
final BeanManager beanManager = cdiContainer.getBeanManager();
final CreationalContext creationalContext = beanManager.createCreationalContext(null);
final AnnotatedType annotatedType = beanManager.createAnnotatedType(this.getClass());
final InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
injectionTarget.inject(this, creationalContext);
}
}
示例10: projectInjectionTarget
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
private <T> void projectInjectionTarget(MybatisExtension extension, Type type) {
ProcessInjectionTarget<T> event = mock(ProcessInjectionTarget.class);
InjectionTarget<T> injectTarget = mock(InjectionTarget.class);
Set<InjectionPoint> injectionPoints = new HashSet<InjectionPoint>();
InjectionPoint injectionPoint = mock(InjectionPoint.class);
Annotated annotated = mock(Annotated.class);
when(injectionPoint.getAnnotated()).thenReturn(annotated);
when(annotated.getBaseType()).thenReturn(type);
when(annotated.getAnnotations()).thenReturn(new HashSet<Annotation>());
injectionPoints.add(injectionPoint);
when(event.getInjectionTarget()).thenReturn(injectTarget);
when(injectTarget.getInjectionPoints()).thenReturn(injectionPoints);
extension.processInjectionTarget(event);
}
示例11: injectFields
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
/**
* Performs dependency injection on an instance. Useful for instances which aren't managed by CDI.
* <p/>
* <b>Attention:</b><br/>
* The resulting instance isn't managed by CDI; only fields annotated with @Inject get initialized.
*
* @param instance current instance
* @param <T> current type
*
* @return instance with injected fields (if possible - or null if the given instance is null)
*/
@SuppressWarnings("unchecked")
public static <T> T injectFields(T instance)
{
if (instance == null)
{
return null;
}
BeanManager beanManager = getBeanManager();
CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
AnnotatedType<T> annotatedType = beanManager.createAnnotatedType((Class<T>) instance.getClass());
InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
injectionTarget.inject(instance, creationalContext);
return instance;
}
示例12: withProducer
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private void withProducer(final Object o, final BiConsumer<Bean, InjectionTarget> consumer) {
final Bean<Object> bean = resolveUnique((Class<Object>) o.getClass());
if (!(bean instanceof AbstractClassBean)) {
throw new TestEEfiException("Injection of " + bean + " is not supported");
}
consumer.accept(bean, ((AbstractClassBean) bean).getProducer());
}
示例13: inject
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的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;
}
示例14: captureProducerTypes
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的package包/类
@Test
public void captureProducerTypes() {
final ProcessInjectionPoint<?, ?> pip = mock(ProcessInjectionPoint.class);
final InjectionPoint ip = mock(InjectionPoint.class);
when(pip.getInjectionPoint()).thenReturn(ip);
final Annotated annotated = mock(Annotated.class);
when(ip.getAnnotated()).thenReturn(annotated);
when(annotated.isAnnotationPresent(Body.class)).thenReturn(true);
when(ip.getType()).thenReturn(COLLECTION.getClass());
@SuppressWarnings("unchecked")
final InjectionTarget<Object> injectionTarget = mock(InjectionTarget.class);
when(this.beanManager.createInjectionTarget(Mockito.any())).thenReturn(injectionTarget);
final Member member = mock(Member.class);
when(ip.getMember()).thenReturn(member);
final List<Bean<?>> found = ReflectionUtil.get(this.extension, "found");
assertTrue(found.isEmpty());
this.extension.captureProducerTypes(pip, this.beanManager);
assertEquals(1, found.size());
verify(pip, times(2)).getInjectionPoint();
verify(ip).getAnnotated();
verify(annotated).isAnnotationPresent(Body.class);
verify(ip).getType();
verify(ip).getMember();
verify(member).getName();
verify(ip).getQualifiers();
verify(injectionTarget).getInjectionPoints();
verify(this.beanManager).createInjectionTarget(Mockito.any());
verifyNoMoreInteractions(pip, ip, annotated, injectionTarget, member);
}
示例15: inject
import javax.enterprise.inject.spi.InjectionTarget; //导入依赖的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;
}