本文整理汇总了Java中javax.enterprise.inject.spi.Bean类的典型用法代码示例。如果您正苦于以下问题:Java Bean类的具体用法?Java Bean怎么用?Java Bean使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Bean类属于javax.enterprise.inject.spi包,在下文中一共展示了Bean类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldDeployDefaultCamelContext
import javax.enterprise.inject.spi.Bean; //导入依赖的package包/类
private boolean shouldDeployDefaultCamelContext(Set<Bean<?>> beans) {
return beans.stream()
// Is there a Camel bean with the @Default qualifier?
// Excluding internal components...
.filter(bean -> !bean.getBeanClass().getPackage().equals(getClass().getPackage()))
.filter(hasType(CamelContextAware.class).or(hasType(Component.class))
.or(hasType(RouteContainer.class).or(hasType(RoutesBuilder.class))))
.map(Bean::getQualifiers)
.flatMap(Set::stream)
.filter(isEqual(DEFAULT))
.findAny()
.isPresent()
// Or a bean with Camel annotations?
|| concat(camelBeans.stream().map(AnnotatedType::getFields),
camelBeans.stream().map(AnnotatedType::getMethods))
.flatMap(Set::stream)
.map(Annotated::getAnnotations)
.flatMap(Set::stream)
.filter(isAnnotationType(Consume.class).and(a -> ((Consume) a).context().isEmpty())
.or(isAnnotationType(BeanInject.class).and(a -> ((BeanInject) a).context().isEmpty()))
.or(isAnnotationType(EndpointInject.class).and(a -> ((EndpointInject) a).context().isEmpty()))
.or(isAnnotationType(Produce.class).and(a -> ((Produce) a).context().isEmpty()))
.or(isAnnotationType(PropertyInject.class).and(a -> ((PropertyInject) a).context().isEmpty())))
.findAny()
.isPresent()
// Or an injection point for Camel primitives?
|| beans.stream()
// Excluding internal components...
.filter(bean -> !bean.getBeanClass().getPackage().equals(getClass().getPackage()))
.map(Bean::getInjectionPoints)
.flatMap(Set::stream)
.filter(ip -> getRawType(ip.getType()).getName().startsWith("org.apache.camel"))
.map(InjectionPoint::getQualifiers)
.flatMap(Set::stream)
.filter(isAnnotationType(Uri.class).or(isAnnotationType(Mock.class)).or(isEqual(DEFAULT)))
.findAny()
.isPresent();
}
示例2: newInstance
import javax.enterprise.inject.spi.Bean; //导入依赖的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;
}
示例3: unique
import javax.enterprise.inject.spi.Bean; //导入依赖的package包/类
private <T> T unique(final Class clazz, final Collection<T> set) {
if (set.isEmpty()) {
final Set<Bean<?>> allBeans = container().beanDeploymentArchives().values().stream()
.map(BeanManagerImpl::getBeans)
.flatMap(Collection::stream)
.collect(toSet());
throw new TestEEfiException("No matching bean found for class "
+ clazz.getName()
+ ", available beans are: "
+ allBeans
);
} else if (set.size() > 1) {
throw new TestEEfiException("Multiple ambiguous beans found for class " + clazz.getName());
} else {
return set.iterator().next();
}
}
示例4: factory
import javax.enterprise.inject.spi.Bean; //导入依赖的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();
}
};
}
};
}
示例5: testIfExists
import javax.enterprise.inject.spi.Bean; //导入依赖的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
示例6: testProducer
import javax.enterprise.inject.spi.Bean; //导入依赖的package包/类
@Test
public void testProducer() {
logger.info("starting producer spi test");
CDI<Object> cdi = current();
BeanManager beanManager = cdi.select(BeanManager.class).get();
AnnotatedField<? super AnotherFactory> field = this
.<AnotherFactory, AnnotatedField<AnotherFactory>>getAnnotatedMember(AnotherFactory.class, "jessie",
beanManager);
Bean<AnotherFactory> declaringBean = cast(beanManager.resolve(beanManager.getBeans(AnotherFactory.class)));
ProducerFactory<AnotherFactory> producerFactory = beanManager.getProducerFactory(field, declaringBean);
Producer<Toy> producer = cast(producerFactory.createProducer(null));
assertNotNull(producer);
assertTrue(producer.getInjectionPoints().isEmpty());
Toy jessie = producer.produce(beanManager.<Toy>createCreationalContext(null));
assertEquals("Jessie", jessie.getName());
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:18,代码来源:InjectSPITestCase.java
示例7: decorateContext
import javax.enterprise.inject.spi.Bean; //导入依赖的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);
}
示例8: decorateUnit
import javax.enterprise.inject.spi.Bean; //导入依赖的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);
}
示例9: getServices
import javax.enterprise.inject.spi.Bean; //导入依赖的package包/类
/**
* Retrieves the services that are available for use with the description for each service.
* The Services are determined by looking up all of the implementations of the
* Customer Service interface that are using the DataService qualifier annotation.
* The DataService annotation contains the service name and description information.
* @return Map containing a list of services available and a description of each one.
*/
public Map<String,String> getServices (){
TreeMap<String,String> services = new TreeMap<String,String>();
logger.fine("Getting CustomerService Impls");
Set<Bean<?>> beans = beanManager.getBeans(CustomerService.class,new AnnotationLiteral<Any>() {
private static final long serialVersionUID = 1L;});
for (Bean<?> bean : beans) {
for (Annotation qualifer: bean.getQualifiers()){
if(DataService.class.getName().equalsIgnoreCase(qualifer.annotationType().getName())){
DataService service = (DataService) qualifer;
logger.fine(" name="+service.name()+" description="+service.description());
services.put(service.name(), service.description());
}
}
}
return services;
}
示例10: getInstancesByType
import javax.enterprise.inject.spi.Bean; //导入依赖的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;
}
示例11: getInstancesByAnnotation
import javax.enterprise.inject.spi.Bean; //导入依赖的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;
}
示例12: createBeanAdapter
import javax.enterprise.inject.spi.Bean; //导入依赖的package包/类
/**
*
* @param ip
* @param beanManager
* @return
*/
private <T> Bean<T> createBeanAdapter(InjectionPoint ip, BeanManager beanManager) {
final Type type = ip.getType();
final Class<T> rawType = ReflectionUtils.getRawType(type);
final ContextualLifecycle<T> lifecycleAdapter = new BodyLifecycle<T>(ip, beanManager);
final BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager)
.readFromType(new AnnotatedTypeBuilder<T>().readFromType(rawType).create())
.beanClass(Body.class) // see https://issues.jboss.org/browse/WELD-2165
.name(ip.getMember().getName())
.qualifiers(ip.getQualifiers())
.beanLifecycle(lifecycleAdapter)
.scope(Dependent.class)
.passivationCapable(false)
.alternative(false)
.nullable(true)
.id("BodyBean#" + type.toString())
.addType(type); //java.lang.Object needs to be present (as type) in any case
return beanBuilder.create();
}
示例13: initialiseContexts
import javax.enterprise.inject.spi.Bean; //导入依赖的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);
}
示例14: getBean
import javax.enterprise.inject.spi.Bean; //导入依赖的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;
}
示例15: afterProcessViewEvent
import javax.enterprise.inject.spi.Bean; //导入依赖的package包/类
/**
* Perform the work we need to do at AfterProcessViewEvent time.
*
* @param event the event.
*/
public void afterProcessViewEvent(@Observes AfterProcessViewEvent event) {
if (request.getAttribute(SCOPE_ID) != null) {
String scopeId = (String) request.getAttribute(SCOPE_ID);
HttpSession session = request.getSession();
final String sessionKey = SCOPE_ID + "-" + scopeId;
Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
if (null != scopeMap) {
scopeMap.entrySet().stream().forEach((entrySet) -> {
String key = entrySet.getKey();
Object value = entrySet.getValue();
if (key.startsWith(INSTANCE)) {
BeanManager beanManager = CdiUtils.getApplicationBeanManager();
Bean<?> bean = beanManager.resolve(beanManager.getBeans(value.getClass()));
destroy(bean);
}
});
scopeMap.clear();
session.removeAttribute(sessionKey);
}
}
}