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

x33g5p2x  于2022-01-18 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(157)

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

CDI介绍

[英]Provides access to the current container.

CDI implements Instance and therefore might be used to perform programmatic lookup. If no qualifier is passed to #select method, the @Default qualifier is assumed.
[中]提供对当前容器的访问。
CDI实现实例,因此可用于执行编程查找。如果没有向#select方法传递限定符,则假定@Default限定符。

代码示例

代码示例来源:origin: oracle/helidon

static synchronized MetricRegistry getMetricRegistry() {
  if (metricRegistry == null) {
    metricRegistry = CDI.current().select(MetricRegistry.class).get();
  }
  return metricRegistry;
}

代码示例来源:origin: javax.enterprise/cdi-api

/**
 * Create an injector for the given class, using the current bean manager
 * @param clazz class of the unmanaged instances
 */
public Unmanaged(Class<T> clazz) {
  this(CDI.current().getBeanManager(), clazz);
}

代码示例来源:origin: javax.enterprise/cdi-api

/**
 *
 * Obtain the {@link CDIProvider} the user set with {@link #setCDIProvider(CDIProvider)}, or if it wasn't set, use the
 * serviceloader the retrieve the {@link CDIProvider} with the highest priority.
 *
 * @return the {@link CDIProvider} set by user or retrieved by serviceloader
 */
private static CDIProvider getCDIProvider() {
  if (configuredProvider != null) {
    return configuredProvider;
  } else {
    // Discover providers and cache
    if (discoveredProviders == null) {
      synchronized (lock) {
        if (discoveredProviders == null) {
          findAllProviders();
        }
      }
    }
    configuredProvider = discoveredProviders.stream()
        .filter(c -> c.getCDI() != null)
        .findFirst().orElseThrow(() -> new IllegalStateException("Unable to access CDI"));
    return configuredProvider;
  }
}

代码示例来源:origin: HotswapProjects/HotswapAgent

if (CDI.current().getBeanManager() instanceof BeanManagerImpl) {
  beanManager = ((BeanManagerImpl) CDI.current().getBeanManager()).unwrap();
} else {
  beanManager = ((BeanManagerProxy) CDI.current().getBeanManager()).unwrap();

代码示例来源:origin: org.jboss.weld.se/weld-se

@Override
public void sessionDestroyed(HttpSessionEvent event) {
  final ServletContext ctx = event.getSession().getServletContext();
  // First try to use the context id obtained from the servlet context (OSGi, Servlet containers, etc.)
  if (beanManager == null) {
    synchronized (this) {
      if (beanManager == null) {
        String contextId = ctx.getInitParameter(Container.CONTEXT_ID_KEY);
        if (contextId != null) {
          List<BeanManagerImpl> managers = new ArrayList<BeanManagerImpl>(Container.instance(contextId).beanDeploymentArchives().values());
          Collections.sort(managers, BeanManagers.ID_COMPARATOR);
          beanManager = managers.get(0);
        }
      }
      // servlet containers may not be able to inject fields in a servlet listener
      if (beanManager == null) {
        beanManager = BeanManagerProxy.unwrap(CDI.current().getBeanManager());
      }
    }
  }
  if (!getSessionContext().isActive()) {
    HttpSessionDestructionContext context = getHttpSessionDestructionContext();
    context.associate(event.getSession());
    context.activate();
  }
}

代码示例来源:origin: org.eclipse.microprofile.fault-tolerance/microprofile-fault-tolerance-tck

private synchronized AsyncCaller getExecutor() {
  if (executor == null) {
    // Lookup the AsyncCaller bean instance from CDI
    BeanManager bm = CDI.current().getBeanManager();
    Bean<?> asyncCallerBean = bm.resolve(bm.getBeans(AsyncCaller.class));
    executor = (AsyncCaller) bm.getReference(asyncCallerBean, AsyncCaller.class, bm.createCreationalContext(null));
  }
  return executor;
}

代码示例来源:origin: com.github.javawithmarcus.wicket-cdi-1.1/wicket-cdi-1.1-core

@SuppressWarnings("unchecked")
private NonContextual(Class<? extends T> clazz) {
  BeanManager manager = CDI.current().getBeanManager();
  AnnotatedType<? extends T> type = manager.createAnnotatedType(clazz);
  this.it = (InjectionTarget<T>) manager.createInjectionTarget(type);
}

代码示例来源:origin: org.glassfish/javax.faces

if (context.getResource("/WEB-INF/faces-config.xml") != null) {
  return true;
CDI<Object> cdi = null;
try {
  cdi = CDI.current();
    Instance<CdiExtension> extension = cdi.select(CdiExtension.class);
    if (!extension.isAmbiguous() && !extension.isUnsatisfied()) {
      return extension.get().isAddBeansForJSFImplicitObjects();

代码示例来源:origin: org.glassfish.soteria/javax.security.enterprise

cdi = CDI.current();
    logger.log(INFO, "Initializing Soteria {0} for context ''{1}''", new Object[]{version, ctx.getContextPath()});
CdiExtension cdiExtension = cdi.select(CdiExtension.class).get();
  ctx.addListener(this);

代码示例来源:origin: weld/weld-vertx

@Test
public void testContextBean() {
  BeanManager beanManager = CDI.current().getBeanManager();
  Bean<?> bean = beanManager.resolve(beanManager.getBeans(Context.class));
  assertTrue(bean.getTypes().contains(Context.class));
}

代码示例来源:origin: org.glassfish.ozark/ozark

@Override
public void configure(FeatureContext context) {
  final Configuration config = context.getConfiguration();
  if (config.isRegistered(ViewResponseFilter.class)) {
    return;     // already registered!
  }
  final boolean enableOzark = config.getClasses().stream().anyMatch(this::isController)
      || config.getInstances().stream().map(o -> o.getClass()).anyMatch(this::isController);
  if (enableOzark) {
    context.register(ViewRequestFilter.class);
    context.register(ViewResponseFilter.class);
    context.register(ViewableWriter.class);
    context.register(BindingInterceptorImpl.class);
    context.register(OzarkModelProcessor.class);
    context.register(CsrfValidateInterceptor.class);
    context.register(CsrfProtectFilter.class);
    // Initialize application config object in Mvc class
    final BeanManager bm = CDI.current().getBeanManager();
    final MvcContextImpl mvc = (MvcContextImpl) newBean(bm, MvcContextImpl.class);
    mvc.setConfig(config);
    mvc.setContextPath(servletContext.getContextPath());
  }
}

代码示例来源:origin: FroMage/redpipe

@Override
public Completable init() {
  return Completable.defer(() -> {
    // Setup the Vertx-CDI integration
    VertxExtension vertxExtension = CDI.current().select(VertxExtension.class).get();
    BeanManager beanManager = CDI.current().getBeanManager();
    // has to be done in a blocking thread
    Vertx vertx = AppGlobals.get().getVertx();
    return vertx.rxExecuteBlocking(future -> {
      vertxExtension.registerConsumers(vertx.getDelegate(), BeanManagerProxy.unwrap(beanManager).event());
      future.complete();
    }).ignoreElement();
  });
}

代码示例来源:origin: com.github.javawithmarcus.wicket-cdi-1.1/wicket-cdi-1.1-core

/**
 * Injects the instance
 * 
 * @param instance
 */
public void inject(T instance) {
  CreationalContext<T> cc = CDI.current().getBeanManager().createCreationalContext(null);
  it.inject(instance, cc);
}

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

public static Set<Bean<?>> getBeans(Class<?> clazz) {
    return CDI.current().getBeanManager().getBeans(clazz);
  }
}

代码示例来源:origin: org.apache.bval/bval-jsr

private <T> boolean computeIsConstructorValidated(Class<T> targetClass, Constructor<T> ctor) {
  final AnnotatedType<T> annotatedType =
    CDI.current().getBeanManager().createAnnotatedType(ctor.getDeclaringClass());
  final ValidateOnExecution annotation =
    annotatedType.getConstructors().stream().filter(ac -> ctor.equals(ac.getJavaMember())).findFirst()
      .map(ac -> ac.getAnnotation(ValidateOnExecution.class))
      .orElseGet(() -> ctor.getAnnotation(ValidateOnExecution.class));
  final Set<ExecutableType> validatedExecutableTypes =
    annotation == null ? classConfiguration : ExecutableTypes.interpret(annotation.type());
  return validatedExecutableTypes.contains(ExecutableType.CONSTRUCTORS);
}

代码示例来源:origin: oracle/helidon

private Optional<SeContainer> currentContainter() {
  try {
    CDI<Object> current = CDI.current();
    STARTUP_LOGGER.finest("CDI.current()");
    if (null == current) {
      return Optional.empty();
    }
    if (current instanceof SeContainer) {
      SeContainer currentSe = (SeContainer) current;
      if (currentSe.isRunning()) {
        return Optional.of(currentSe);
      } else {
        return Optional.empty();
      }
    } else {
      throw new MpException("Running in a non-SE CDI Container: " + current.getClass().getName());
    }
  } catch (IllegalStateException e) {
    return Optional.empty();
  }
}

代码示例来源:origin: weld/core

@Override
public void sessionDestroyed(HttpSessionEvent event) {
  final ServletContext ctx = event.getSession().getServletContext();
  // First try to use the context id obtained from the servlet context (OSGi, Servlet containers, etc.)
  if (beanManager == null) {
    synchronized (this) {
      if (beanManager == null) {
        String contextId = ctx.getInitParameter(Container.CONTEXT_ID_KEY);
        if (contextId != null) {
          List<BeanManagerImpl> managers = new ArrayList<BeanManagerImpl>(Container.instance(contextId).beanDeploymentArchives().values());
          Collections.sort(managers, BeanManagers.ID_COMPARATOR);
          beanManager = managers.get(0);
        }
      }
      // servlet containers may not be able to inject fields in a servlet listener
      if (beanManager == null) {
        beanManager = BeanManagerProxy.unwrap(CDI.current().getBeanManager());
      }
    }
  }
  if (!getSessionContext().isActive()) {
    HttpSessionDestructionContext context = getHttpSessionDestructionContext();
    context.associate(event.getSession());
    context.activate();
  }
}

代码示例来源:origin: javax/javaee-web-api

@SuppressWarnings("unchecked")
private Map<Class<?>, Class<? extends DataModel<?>>> getDataModelClassesMap(CDI<Object> cdi) {
  BeanManager beanManager = cdi.getBeanManager();
  // Get the Map with classes for which a custom DataModel implementation is available from CDI
  Bean<?> bean = beanManager.resolve(beanManager.getBeans("comSunFacesDataModelClassesMap"));
  Object beanReference = beanManager.getReference(bean, Map.class, beanManager.createCreationalContext(bean));
  
  return (Map<Class<?>, Class<? extends DataModel<?>>>) beanReference;
}

代码示例来源:origin: org.apache.meecrowave/meecrowave-junit

private static CreationalContext<Object> doInject(final Object instance) {
  final BeanManager bm = CDI.current().getBeanManager();
  final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
  final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
  final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
  injectionTarget.inject(instance, creationalContext);
  return creationalContext;
}

代码示例来源:origin: org.glassfish/jakarta.faces

if (context.getResource("/WEB-INF/faces-config.xml") != null) {
  return true;
CDI<Object> cdi = null;
try {
  cdi = CDI.current();
    Instance<CdiExtension> extension = cdi.select(CdiExtension.class);
    if (!extension.isAmbiguous() && !extension.isUnsatisfied()) {
      return extension.get().isAddBeansForJSFImplicitObjects();

相关文章