javax.enterprise.inject.spi.Bean类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(221)

本文整理了Java中javax.enterprise.inject.spi.Bean类的一些代码示例,展示了Bean类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bean类的具体详情如下:
包路径:javax.enterprise.inject.spi.Bean
类名称:Bean

Bean介绍

[英]Represents an javax.enterprise.inject. This interface defines everything the container needs to manage instances of the bean.
[中]表示一个javax。企业注射这个接口定义了容器管理bean实例所需的一切。

代码示例

代码示例来源:origin: jersey/jersey

@Override
@SuppressWarnings("unchecked")
public <T> List<ServiceHolder<T>> getAllServiceHolders(Class<T> contractOrImpl, Annotation... qualifiers) {
  List<ServiceHolder<T>> result = new ArrayList<>();
  for (Bean<?> bean : beanManager.getBeans(contractOrImpl, qualifiers)) {
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    T reference = (T) beanManager.getReference(bean, contractOrImpl, ctx);
    int rank = 1;
    if (bean instanceof JerseyBean) {
      rank = ((JerseyBean) bean).getRank();
    }
    result.add(new ServiceHolderImpl<>(reference, (Class<T>) bean.getBeanClass(), bean.getTypes(), rank));
  }
  return result;
}

代码示例来源:origin: jersey/jersey

/**
   * Get me scope of a bean corresponding to given class.
   *
   * @param beanClass bean class in question.
   * @param beanManager actual bean manager.
   * @return actual bean scope or null, if the scope could not be determined.
   */
  public static Class<? extends Annotation> getBeanScope(final Class<?> beanClass, final BeanManager beanManager) {
    final Set<Bean<?>> beans = beanManager.getBeans(beanClass);
    if (beans.isEmpty()) {
      return null;
    }
    for (Bean b : beans) {
      return b.getScope();
    }
    return null;
  }
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@SuppressWarnings("unchecked")
<X> void processBean(@Observes ProcessBean<X> processBean) {
  Bean<X> bean = processBean.getBean();
  for (Type type : bean.getTypes()) {
    if (type instanceof Class<?> && MongoOperations.class.isAssignableFrom((Class<?>) type)) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Discovered %s with qualifiers %s.", MongoOperations.class.getName(),
            bean.getQualifiers()));
      }
      // Store the EntityManager bean using its qualifiers.
      mongoOperations.put(new HashSet<Annotation>(bean.getQualifiers()), (Bean<MongoOperations>) bean);
    }
  }
}

代码示例来源:origin: spring-projects/spring-data-jpa

/**
 * Implementation of a an observer which checks for EntityManager beans and stores them in {@link #entityManagers} for
 * later association with corresponding repository beans.
 *
 * @param <X> The type.
 * @param processBean The annotated type as defined by CDI.
 */
@SuppressWarnings("unchecked")
<X> void processBean(@Observes ProcessBean<X> processBean) {
  Bean<X> bean = processBean.getBean();
  for (Type type : bean.getTypes()) {
    // Check if the bean is an EntityManager.
    if (type instanceof Class<?> && EntityManager.class.isAssignableFrom((Class<?>) type)) {
      Set<Annotation> qualifiers = new HashSet<Annotation>(bean.getQualifiers());
      if (bean.isAlternative() || !entityManagers.containsKey(qualifiers)) {
        LOGGER.debug("Discovered '{}' with qualifiers {}.", EntityManager.class.getName(), qualifiers);
        entityManagers.put(qualifiers, (Bean<EntityManager>) bean);
      }
    }
  }
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertion(section = APPLICATION_CONTEXT_EE, id = "dh")
public void testApplicationContextActivePreDestroy() {
  Bean<Foo> bean = getUniqueBean(Foo.class);
  CreationalContext<Foo> ctx = getCurrentManager().createCreationalContext(bean);
  Foo foo = bean.create(ctx);
  assertFalse(foo.isBarAlone());
  bean.destroy(foo, ctx);
  assertTrue(bar.isAlone());
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertions({ @SpecAssertion(section = DEFAULT_NAME, id = "fc") })
public void testDefaultNamed() {
  Bean<Tarantula> tarantulaBean = getUniqueBean(Tarantula.class, PET_LITERAL);
  assertEquals(tarantulaBean.getName(), "producedPetTarantula");
  // Any, Pet, Named
  assertTrue(annotationSetMatches(tarantulaBean.getQualifiers(), Any.Literal.INSTANCE, PET_LITERAL, NamedLiteral.of(
      "producedPetTarantula")));
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertions({ @SpecAssertion(section = BM_OBTAIN_BEAN_BY_TYPE, id = "c") })
public void testNoBindingImpliesCurrent() {
  Set<Bean<?>> beans = getCurrentManager().getBeans(SimpleBean.class);
  assertEquals(beans.size(), 1);
  assertTrue(beans.iterator().next().getQualifiers().contains(Default.Literal.INSTANCE));
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test(groups = INTEGRATION)
@SpecAssertion(section = INJECTION_POINT, id = "aa")
public void testContextualEjbInjectionPointGetBean() throws Exception {
  Bean<?> bean = baz.get().getFoo().getInjectionPoint().getBean();
  assertNotNull(bean);
  assertEquals(bean.getBeanClass(), Baz.class);
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test(groups = INTEGRATION)
@SpecAssertions({ @SpecAssertion(section = DIRECT_AND_INDIRECT_SPECIALIZATION_EE, id = "k") })
public void testSpecializingBeanHasNameOfSpecializedBean() {
  String expectedName = "farmer";
  Set<Bean<?>> beans = getCurrentManager().getBeans(expectedName);
  assertEquals(beans.size(), 1);
  Bean<?> farmerBean = beans.iterator().next();
  assertEquals(farmerBean.getName(), expectedName);
  assertEquals(farmerBean.getBeanClass(), LazyFarmer.class);
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertions({ @SpecAssertion(section = ALTERNATIVE_METADATA_SOURCES, id = "hf"),
    @SpecAssertion(section = ALTERNATIVE_METADATA_SOURCES, id = "ad") })
public void testGetBaseTypeUsedToDetermineTypeOfDisposerInjectionPoint() {
  Bean<Bill> bill = getBeans(Bill.class, new CheapLiteral()).iterator().next();
  CreationalContext<Bill> context = getCurrentManager().createCreationalContext(bill);
  Bill instance = getCurrentManager().getContext(bill.getScope()).get(bill);
  bill.destroy(instance, context);
  assertEquals(getContextualReference(Grocery.class, Any.Literal.INSTANCE).getDisposerFruit().getMetadata().getType(),
      TropicalFruit.class);
  assertTrue(GroceryWrapper.isGetBaseTypeOfBillDisposerParameterUsed());
  // ad assertion
  assertTrue(getContextualReference(Grocery.class, Any.Literal.INSTANCE).getDisposerFruit().getMetadata().getQualifiers().contains(Any.Literal.INSTANCE));
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@SuppressWarnings("serial")
@Test
@SpecAssertions({ @SpecAssertion(section = BEFORE_BEAN_DISCOVERY, id = "b") })
public void testAddAnnotatedTypeWithConfigurator() {
  Bean<Baz> bazBean = getUniqueBean(Baz.class, Pro.ProLiteral.INSTANCE);
  assertEquals(bazBean.getScope(), RequestScoped.class);
  Baz baz = (Baz) getCurrentManager().getReference(bazBean, Baz.class, getCurrentManager().createCreationalContext(bazBean));
  assertFalse(baz.getBarInstance().isUnsatisfied());
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertions({ @SpecAssertion(section = ALTERNATIVE_METADATA_SOURCES, id = "j"),
    @SpecAssertion(section = ALTERNATIVE_METADATA_SOURCES, id = "ac") })
public void testGetBaseTypeUsedToDetermineTypeOfDisposalParameter() throws NoSuchMethodException {
  Bean<Carrot> carrot = getBeans(Carrot.class, new CheapLiteral()).iterator().next();
  CreationalContext<Carrot> context = getCurrentManager().createCreationalContext(carrot);
  Carrot instance = getCurrentManager().getContext(carrot.getScope()).get(carrot, context);
  carrot.destroy(instance, context);
  // ac assertion - disposal method called after adding extra qualifier to disposer parameter
  assertNotNull(getContextualReference(Grocery.class, Any.Literal.INSTANCE).getWrappedDisposalParameter());
  assertEquals(getContextualReference(Grocery.class, Any.Literal.INSTANCE).getWrappedDisposalParameter().getClass(),
      Carrot.class);
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test(groups = { JAX_WS, JAVAEE_FULL })
  @SpecAssertion(section = RESOURCE_LIFECYCLE, id = "le")
  public void testInjectionOfWebServiceReference() {
    Bean<ManagedBean> managedBean = getUniqueBean(ManagedBean.class);
    CreationalContext<ManagedBean> creationalContext = getCurrentManager().createCreationalContext(managedBean);
    ManagedBean instance = managedBean.create(creationalContext);
    Assert.assertNotNull(instance.getTranslator());
    Assert.assertEquals(instance.getTranslator().translate(), TranslatorEndpoint.MESSAGE);
  }
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertion(section = INTERCEPTION_FACTORY, id = "g")
public void interceptionFactoryBeanIsAvailable() {
  Bean<?> interceptionFactoryBean = getCurrentManager().resolve(getCurrentManager().getBeans(InterceptionFactory.class));
  Assert.assertEquals(Dependent.class, interceptionFactoryBean.getScope());
  Assert.assertEquals(Stream.of(Default.Literal.INSTANCE, Any.Literal.INSTANCE).collect(Collectors.toSet()), interceptionFactoryBean.getQualifiers());
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertion(section = ALTERNATIVE_METADATA_SOURCES, id = "ab")
public void testDisposesIsAppliedToMethodParameter() {
  Bean<Yogurt> yogurt = getBeans(Yogurt.class, new ExpensiveLiteral()).iterator().next();
  CreationalContext<Yogurt> context = getCurrentManager().createCreationalContext(yogurt);
  Yogurt instance = getCurrentManager().getContext(yogurt.getScope()).get(yogurt, context);
  yogurt.destroy(instance, context);
  assertTrue(Grocery.isDisposerMethodCalled());
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
  @SpecAssertion(section = PROCESS_BEAN_ATTRIBUTES, id = "bca")
  public void configuratorInitializedWithOriginalBeanAttributes() {
    Bean<Mace> configuredBean = getUniqueBean(Mace.class);
    BeanAttributes<Mace> originalBA = getCurrentManager().getExtension(ProcessBeanAttributesObserver.class).getOriginalBA();
    assertEquals(configuredBean.getTypes(), originalBA.getTypes());
    assertEquals(configuredBean.getQualifiers(), originalBA.getQualifiers());
    assertEquals(configuredBean.getStereotypes(), originalBA.getStereotypes());
    assertEquals(configuredBean.getScope(), originalBA.getScope());
  }
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test
@SpecAssertions({ @SpecAssertion(section = DECLARING_MANAGED_BEAN, id = "bd"), @SpecAssertion(section = BEAN, id = "ba") })
public void testMultipleStereotypes() {
  Bean<ComplicatedTuna> tunaBean = getBeans(ComplicatedTuna.class).iterator().next();
  assertEquals(tunaBean.getScope(), RequestScoped.class);
  assertEquals(tunaBean.getName(), "complicatedTuna");
}

代码示例来源:origin: org.jboss.cdi.tck/cdi-tck-impl

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertion(section = CONTEXT, id = "t")
public void testNothingHappensIfNoInstanceToDestroy(ApplicationScopedComponent application) {
  Bean<?> bean = getUniqueBean(ApplicationScopedComponent.class);
  AlterableContext context = (AlterableContext) getCurrentManager().getContext(bean.getScope());
  AbstractComponent.reset();
  application.setValue("value");
  context.destroy(bean);
  assertTrue(AbstractComponent.isDestroyed());
  context.destroy(bean); // make sure subsequent calls do not raise exception
  context.destroy(bean);
}

代码示例来源:origin: resteasy/Resteasy

@Override
public CompletionStage<Object> construct(boolean unwrapAsync)
{
 Set<Bean<?>> beans = manager.getBeans(type);
 if (beans.size() > 1)
 {
   Set<Bean<?>> modifiableBeans = new HashSet<Bean<?>>();
   modifiableBeans.addAll(beans);
   // Ambiguous dependency may occur if a resource has subclasses
   // Therefore we remove those beans
   for (Iterator<Bean<?>> iterator = modifiableBeans.iterator(); iterator.hasNext();)
   {
    Bean<?> bean = iterator.next();
    if (!bean.getBeanClass().equals(type) && !bean.isAlternative())
    {
      // remove Beans that have clazz in their type closure but not as a base class
      iterator.remove();
    }
   }
   beans = modifiableBeans;
 }
 if (LogMessages.LOGGER.isDebugEnabled()) //keep this check for performance reasons, as toString() is expensive on CDI Bean
 {
   LogMessages.LOGGER.debug(Messages.MESSAGES.beansFound(type, beans));
 }
 Bean<?> bean = manager.resolve(beans);
 CreationalContext<?> context = manager.createCreationalContext(bean);
 return CompletableFuture.completedFuture(manager.getReference(bean, type, context));
}

代码示例来源:origin: wildfly/wildfly

@Override
public Object create(final String ref, Class<?> cls, final ClassLoader classLoader) throws Exception {
  final BeanManager beanManager = getBeanManager();
  final Bean<?> bean = getBean(ref, beanManager);
  if (bean == null) {
    return null;
  }
  final CreationalContext<?> context = beanManager.createCreationalContext(bean);
  final Object result = beanManager.getReference(bean, bean.getBeanClass(), context);
  contexts.put(result, new Holder(bean, context));
  return result;
}

相关文章