javax.enterprise.inject.spi.Unmanaged.<init>()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(123)

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

Unmanaged.<init>介绍

[英]Create an injector for the given class, using the current bean manager
[中]使用当前的bean管理器为给定的类创建一个注入器

代码示例

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

@Override
public <T> T createAndInitialize(Class<T> createMe) {
  if (container != null) {
    Unmanaged.UnmanagedInstance<T> unmanaged = new Unmanaged<>(createMe).newInstance();
    return unmanaged.produce()
        .inject()
        .postConstruct()
        .get();
  } else {
    // TODO: method is invoked before #completeRegistration - creates AutoDiscoverable, ForcedAutoDiscoverable.
    // Hack: creates an object with default constructor and without an injection.
    try {
      Constructor<T> constructor = createMe.getConstructor();
      return constructor.newInstance();
    } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
      throw new RuntimeException("Cannot create an instance of a class: " + createMe, e);
    }
  }
}

代码示例来源:origin: hammock-project/hammock

@Override
  public InstanceHandle<T> createInstance() {
    try {
      return new HammockInstanceHandle<>(new Unmanaged<>(beanManager, clazz).newInstance());
    }
    catch (Exception e) {
      try {
        return new BasicInstanceFactory<T>(clazz.newInstance());
      } catch (Exception ex) {
        throw new RuntimeException("Unable to instantiate "+clazz, ex);
      }
    }
  }
}

代码示例来源:origin: ws.ament.hammock/web-undertow

@Override
  public InstanceHandle<T> createInstance() {
    try {
      return new HammockInstanceHandle<>(new Unmanaged<>(beanManager, clazz).newInstance());
    }
    catch (Exception e) {
      try {
        return new BasicInstanceFactory<T>(clazz.newInstance());
      } catch (Exception ex) {
        throw new RuntimeException("Unable to instantiate "+clazz, ex);
      }
    }
  }
}

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

private UnmanagedInstance<House> createUnmanagedInstance() {
  Unmanaged<House> unmanagedHouse = new Unmanaged<House>(getCurrentManager(), House.class);
  UnmanagedInstance<House> unmanagedHouseInstance = unmanagedHouse.newInstance();
  return unmanagedHouseInstance;
}

代码示例来源:origin: ws.ament.hammock/web-tomcat

@Override
public Object newInstance(Class<?> clazz) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException {
  try {
    Unmanaged.UnmanagedInstance<?> instance = new Unmanaged<>(beanManager, clazz).newInstance();
    instance.produce().inject().postConstruct();
    Object o = instance.get();
    instanceMap.put(o, instance);
    return o;
  } catch (Exception e) {
    return clazz.newInstance();
  }
}

代码示例来源:origin: hammock-project/hammock

@Override
public Object newInstance(Class<?> clazz) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException {
  try {
    Unmanaged.UnmanagedInstance<?> instance = new Unmanaged<>(beanManager, clazz).newInstance();
    instance.produce().inject().postConstruct();
    Object o = instance.get();
    instanceMap.put(o, instance);
    return o;
  } catch (Exception e) {
    return clazz.newInstance();
  }
}

代码示例来源:origin: io.thorntail/microprofile-health

public void afterDeploymentValidation(@Observes final AfterDeploymentValidation abd, BeanManager beanManager) {
  try {
    if (delegate != null) {
      Unmanaged<SmallRyeHealthReporter> unmanagedHealthCheck =
        new Unmanaged<>(beanManager, SmallRyeHealthReporter.class);
      reporterInstance = unmanagedHealthCheck.newInstance();
      reporter =  reporterInstance.produce().inject().postConstruct().get();
      monitor.registerHealthReporter(reporter);
      // THORN-2195: Use the correct TCCL when health checks are obtained
      // In WildFly, the TCCL should always be set to the top-level deployment CL during extension notification
      monitor.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
      log.info(">> Added health reporter bean " + reporter);
      delegate = null;
    }
  } catch (Exception e) {
    throw new RuntimeException("Failed to register health reporter bean", e);
  }
}

代码示例来源:origin: hammock-project/hammock

public Unmanageable(Class<T> clazz) {
  this.instance = new Unmanaged<T>(clazz).newInstance();
  this.instance.produce().inject().postConstruct();
}

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

@Test(groups = INTEGRATION)
@SpecAssertion(section = BM_OBTAIN_UNMANAGED_INSTANCE, id = "b")
public void testObtainNonContextualInstance() {
  Zoo.reset();
  Proboscis.reset();
  Elephant.reset();
  Unmanaged<Zoo> unmanagedZoo = new Unmanaged<Zoo>(Zoo.class);
  UnmanagedInstance<Zoo> unmanagedZooInstance = unmanagedZoo.newInstance();
  Zoo zoo = unmanagedZooInstance.produce().inject().postConstruct().get();
  zoo.build();
  assertTrue(Zoo.postConstructCalled);
  assertTrue(Proboscis.postConstructCalled);
  assertTrue(Elephant.postConstructCalled);
  unmanagedZooInstance.preDestroy().dispose();
  assertTrue(Zoo.preDestroyCalled);
  assertTrue(Proboscis.preDestroyCalled);
  assertFalse(Elephant.preDestroyCalled);
}

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

@Override
public InstanceHandle<T> createInstance() throws InstantiationException {
  Object manager = context.getAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME);
  if (manager instanceof BeanManager) {
    UnmanagedInstance<T> instance = new Unmanaged<T>(BeanManagerProxy.unwrap((BeanManager) manager), clazz).newInstance();
    instance.produce().inject().postConstruct();
    return new WeldInstanceHandle<T>(instance);
  } else {
    // fallback
    return delegate.createInstance();
  }
}

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

@Override
public InstanceHandle<T> createInstance() throws InstantiationException {
  Object manager = context.getAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME);
  if (manager instanceof BeanManager) {
    UnmanagedInstance<T> instance = new Unmanaged<T>(BeanManagerProxy.unwrap((BeanManager) manager), clazz).newInstance();
    instance.produce().inject().postConstruct();
    return new WeldInstanceHandle<T>(instance);
  } else {
    // fallback
    return delegate.createInstance();
  }
}

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

@Test
@SpecAssertions({ @SpecAssertion(section = BM_OBTAIN_UNMANAGED_INSTANCE, id = "a") })
public void testObtainNonContextualInstanceUsingCurrentBeanManager() {
  Builder.reset();
  Nail.reset();
  Hammer.reset();
  Unmanaged<Builder> unmanagedBuilder = new Unmanaged<Builder>(getCurrentManager(), Builder.class);
  UnmanagedInstance<Builder> unmanagedBuilderInstance = unmanagedBuilder.newInstance();
  Builder builder = unmanagedBuilderInstance.produce().inject().postConstruct().get();
  builder.build();
  assertTrue(Builder.postConstructCalled);
  assertTrue(Nail.postConstructCalled);
  assertTrue(Hammer.postConstructCalled);
  unmanagedBuilderInstance.preDestroy().dispose();
  assertTrue(Builder.preDestroyCalled);
  assertTrue(Nail.preDestroyCalled);
  assertFalse(Hammer.preDestroyCalled);
}

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

@Override
public InstanceHandle<T> createInstance() throws InstantiationException {
  Object manager = context.getAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME);
  if (manager instanceof BeanManager) {
    UnmanagedInstance<T> instance = new Unmanaged<T>(BeanManagerProxy.unwrap((BeanManager) manager), clazz).newInstance();
    instance.produce().inject().postConstruct();
    return new WeldInstanceHandle<T>(instance);
  } else {
    // fallback
    return delegate.createInstance();
  }
}

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

Map<String, UnmanagedInstance<?>> ctxToUnmanaged = new ConcurrentHashMap<>();
create(ctx -> {
  Unmanaged<?> unmanaged = new Unmanaged<>(WeldContainer.current().getBeanManager(), beanClass);
  UnmanagedInstance<?> unmanagedInstance = unmanaged.newInstance();
  ctxToUnmanaged.put(ctx.toString(), unmanagedInstance);

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

@Test
@SpecAssertions({ @SpecAssertion(section = BIZ_METHOD_EE, id = "ac") })
public void testNonContextualInstanceIsIntercepted() {
  ToolInterceptor.intercepted = false;
  Unmanaged<Axe> unmanagedAxe = new Unmanaged<Axe>(getCurrentManager(), Axe.class);
  UnmanagedInstance<Axe> unmanagedAxeInstance = unmanagedAxe.newInstance();
  unmanagedAxeInstance.produce().inject().postConstruct().get().cut();
  unmanagedAxeInstance.preDestroy().dispose();
  assertTrue(ToolInterceptor.intercepted);
}

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

Map<String, UnmanagedInstance<?>> ctxToUnmanaged = new ConcurrentHashMap<>();
create(ctx -> {
  Unmanaged<?> unmanaged = new Unmanaged<>(WeldContainer.current().getBeanManager(), beanClass);
  UnmanagedInstance<?> unmanagedInstance = unmanaged.newInstance();
  ctxToUnmanaged.put(ctx.toString(), unmanagedInstance);

代码示例来源:origin: io.smallrye/smallrye-fault-tolerance

@Override
  public T handle(ExecutionContext context) {
    Unmanaged<FallbackHandler<T>> unmanaged = new Unmanaged<>(beanManager, operation.getFallback().get(FallbackConfig.VALUE));
    Unmanaged.UnmanagedInstance<FallbackHandler<T>> unmanagedInstance = unmanaged.newInstance();
    FallbackHandler<T> handler = unmanagedInstance.produce().inject().postConstruct().get();
    try {
      return handler.handle(context);
    } finally {
      // The instance exists to service a single invocation only
      unmanagedInstance.preDestroy().dispose();
    }
  }
};

相关文章