本文整理汇总了Java中javax.enterprise.context.spi.CreationalContext类的典型用法代码示例。如果您正苦于以下问题:Java CreationalContext类的具体用法?Java CreationalContext怎么用?Java CreationalContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CreationalContext类属于javax.enterprise.context.spi包,在下文中一共展示了CreationalContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newInstance
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
private <T> T newInstance(final Bean<T> bean, final ReleaseCallbackHandler handler) {
final CreationalContextImpl<T> ctx = contextFor(bean, handler);
final T instance = bean.create(ctx);
ctx.addDependentInstance(new ContextualInstance<T>() {
@Override
public T getInstance() {
return instance;
}
@Override
public CreationalContext<T> getCreationalContext() {
return ctx;
}
@Override
public Contextual<T> getContextual() {
return bean;
}
});
return instance;
}
示例2: factory
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
private <T> ProducerFactory<T> factory(final Object mock) {
return new ProducerFactory<T>() {
@Override
public <T1> Producer<T1> createProducer(final Bean<T1> bean) {
return new Producer<T1>() {
@Override
public T1 produce(final CreationalContext<T1> ctx) {
return (T1) mock;
}
@Override
public void dispose(final T1 instance) {
}
@Override
public Set<InjectionPoint> getInjectionPoints() {
return Collections.emptySet();
}
};
}
};
}
示例3: getCreationalContext
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
private <T> CreationalContext<T> getCreationalContext() {
CreationalContext<T> creationalContext = new CreationalContext<T>() {
private List<T> applicationBeans = new ArrayList<T>();
private T currentInstance;
@Override
public void push(T incompleteInstance) {
applicationBeans.add(incompleteInstance);
currentInstance = incompleteInstance;
}
@Override
public void release() {
applicationBeans.remove(currentInstance);
}
};
return creationalContext;
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:24,代码来源:ContextSPITestCase.java
示例4: testIfExists
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
/**
* Tests if exists observer injection in a jar archive
*/
@Test
public void testIfExists() {
logger.info("starting if exists event test");
// To test the IF_EXISTS Reception I need to inject the observer bean so
// it will be instantiated and ready to use
Set<Bean<?>> beans = beanManager.getBeans(IfExistsObserver.class);
assertEquals(beans.size(), 1);
@SuppressWarnings("unchecked")
Bean<IfExistsObserver> bean = (Bean<IfExistsObserver>) beans.iterator().next();
CreationalContext<IfExistsObserver> ctx = beanManager.createCreationalContext(bean);
beanManager.getReference(bean, IfExistsObserver.class, ctx);
Bill bill = fire();
assertEquals("The id generation passes through the always and if_exists observers and it is incremented", 10,
bill.getId());
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:19,代码来源:EventTestCase.java
示例5: testInjectionTarget
import javax.enterprise.context.spi.CreationalContext; //导入依赖的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
示例6: decorateContext
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
private <X> AnnotatedField<X> decorateContext(AnnotatedField<X> field) {
final PersistenceContext persistenceContext = field.getAnnotation(PersistenceContext.class);
final UniqueIdentifier identifier = UniqueIdentifierLitteral.random();
Set<Annotation> templateQualifiers = new HashSet<>();
templateQualifiers.add(ServiceLiteral.SERVICE);
if (hasUnitName(persistenceContext)) {
templateQualifiers.add(new FilterLiteral("(osgi.unit.name=" + persistenceContext.unitName() + ")"));
}
Bean<JpaTemplate> bean = manager.getExtension(OsgiExtension.class)
.globalDependency(JpaTemplate.class, templateQualifiers);
Set<Annotation> qualifiers = new HashSet<>();
qualifiers.add(identifier);
Bean<EntityManager> b = new SimpleBean<>(EntityManager.class, Dependent.class, Collections.singleton(EntityManager.class), qualifiers, () -> {
CreationalContext<JpaTemplate> context = manager.createCreationalContext(bean);
JpaTemplate template = (JpaTemplate) manager.getReference(bean, JpaTemplate.class, context);
return EntityManagerProducer.create(template);
});
beans.add(b);
Set<Annotation> fieldAnnotations = new HashSet<>();
fieldAnnotations.add(InjectLiteral.INJECT);
fieldAnnotations.add(identifier);
return new SyntheticAnnotatedField<>(field, fieldAnnotations);
}
示例7: decorateUnit
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
private <X> AnnotatedField<X> decorateUnit(AnnotatedField<X> field) {
final PersistenceUnit persistenceUnit = field.getAnnotation(PersistenceUnit.class);
final UniqueIdentifier identifier = UniqueIdentifierLitteral.random();
Set<Annotation> templateQualifiers = new HashSet<>();
templateQualifiers.add(ServiceLiteral.SERVICE);
if (hasUnitName(persistenceUnit)) {
templateQualifiers.add(new FilterLiteral("(osgi.unit.name=" + persistenceUnit.unitName() + ")"));
}
Bean<EntityManagerFactory> bean = manager.getExtension(OsgiExtension.class)
.globalDependency(EntityManagerFactory.class, templateQualifiers);
Set<Annotation> qualifiers = new HashSet<>();
qualifiers.add(identifier);
Bean<EntityManagerFactory> b = new SimpleBean<>(EntityManagerFactory.class, Dependent.class, Collections.singleton(EntityManagerFactory.class), qualifiers, () -> {
CreationalContext<EntityManagerFactory> context = manager.createCreationalContext(bean);
return (EntityManagerFactory) manager.getReference(bean, EntityManagerFactory.class, context);
});
beans.add(b);
Set<Annotation> fieldAnnotations = new HashSet<>();
fieldAnnotations.add(InjectLiteral.INJECT);
fieldAnnotations.add(identifier);
return new SyntheticAnnotatedField<>(field, fieldAnnotations);
}
示例8: getInstancesByType
import javax.enterprise.context.spi.CreationalContext; //导入依赖的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;
}
示例9: getInstancesByAnnotation
import javax.enterprise.context.spi.CreationalContext; //导入依赖的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;
}
示例10: get
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get();
if (ctx == null) {
// Thread local not set - context is not active!
throw new ContextNotActiveException();
}
ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual);
if (instance == null && creationalContext != null) {
// Bean instance does not exist - create one if we have CreationalContext
instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual);
ctx.put(contextual, instance);
}
return instance != null ? instance.get() : null;
}
示例11: initialiseContexts
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void initialiseContexts() {
final WebSocketContext webSocketContext = mock(WebSocketContext.class);
ReflectionUtil.set(this.extension, "webSocketContext", webSocketContext);
final AfterDeploymentValidation afterDeploymentValidation = mock(AfterDeploymentValidation.class);
final Bean<?> bean = mock(Bean.class);
when(this.beanManager.getBeans(WebSocketSessionHolder.class)).thenReturn(Collections.singleton(bean));
when(this.beanManager.resolve(any(Set.class))).thenReturn(bean);
final CreationalContext creationalContext = mock(CreationalContext.class);
when(this.beanManager.createCreationalContext(bean)).thenReturn(creationalContext);
final WebSocketSessionHolder webSocketSessionHolder = mock(WebSocketSessionHolder.class);
when(this.beanManager.getReference(bean, WebSocketSessionHolder.class, creationalContext)).thenReturn(webSocketSessionHolder);
this.extension.initialiseContexts(afterDeploymentValidation, this.beanManager);
verify(this.beanManager).getBeans(WebSocketSessionHolder.class);
verify(this.beanManager).resolve(any(Set.class));
verify(this.beanManager).createCreationalContext(bean);
verify(this.beanManager).getReference(bean, WebSocketSessionHolder.class, creationalContext);
verify(webSocketContext).init(webSocketSessionHolder);
verifyNoMoreInteractions(webSocketContext, afterDeploymentValidation, bean, creationalContext, webSocketSessionHolder);
}
示例12: getBean
import javax.enterprise.context.spi.CreationalContext; //导入依赖的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;
}
示例13: get
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get();
if (ctx == null) {
// Thread local not set - context is not active!
throw new ContextNotActiveException();
}
ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual);
if (instance == null && creationalContext != null) {
// Bean instance does not exist - create one if we have CreationalContext
instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual);
ctx.put(contextual, instance);
}
return instance != null ? instance.get() : null;
}
示例14: destroy
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
/**
* Destroy the instance.
*
* @param contextual the contextual.
*/
public void destroy(Contextual contextual) {
String scopeId = (String) request.getAttribute(SCOPE_ID);
if (null != scopeId) {
HttpSession session = request.getSession();
if (contextual instanceof PassivationCapable == false) {
throw new RuntimeException("Unexpected type for contextual");
}
PassivationCapable pc = (PassivationCapable) contextual;
final String sessionKey = SCOPE_ID + "-" + scopeId;
Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
if (null != scopeMap) {
Object instance = scopeMap.get(INSTANCE + pc.getId());
CreationalContext<?> creational = (CreationalContext<?>) scopeMap.get(CREATIONAL + pc.getId());
if (null != instance && null != creational) {
contextual.destroy(instance, creational);
creational.release();
}
}
}
}
示例15: get
import javax.enterprise.context.spi.CreationalContext; //导入依赖的package包/类
/**
* Get the instance (create it if it does not exist).
*
* @param <T> the type.
* @param contextual the contextual.
* @param creational the creational.
* @return the instance.
*/
public <T> T get(Contextual<T> contextual, CreationalContext<T> creational) {
T result = get(contextual);
if (result == null) {
String scopeId = (String) request.getAttribute(SCOPE_ID);
if (null == scopeId) {
scopeId = generateScopeId();
}
HttpSession session = request.getSession();
result = contextual.create(creational);
if (contextual instanceof PassivationCapable == false) {
throw new RuntimeException("Unexpected type for contextual");
}
PassivationCapable pc = (PassivationCapable) contextual;
final String sessionKey = SCOPE_ID + "-" + scopeId;
Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
if (null != scopeMap) {
session.setAttribute(sessionKey, scopeMap);
scopeMap.put(INSTANCE + pc.getId(), result);
scopeMap.put(CREATIONAL + pc.getId(), creational);
}
}
return result;
}