org.jboss.weld.environment.se.Weld类的使用及代码示例

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

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

Weld介绍

[英]This builder is a preferred method of booting Weld SE container.

Typical usage looks like this:

WeldContainer container = new Weld().initialize(); 
container.select(Foo.class).get(); 
container.event().select(Bar.class).fire(new Bar()); 
container.shutdown();

The WeldContainer implements AutoCloseable:

try (WeldContainer container = new Weld().initialize()) { 
container.select(Foo.class).get(); 
}

By default, the discovery is enabled so that all beans from all discovered bean archives are considered. However, it's possible to define a "synthetic" bean archive, or the set of bean classes and enablement respectively:

WeldContainer container = new Weld().beanClasses(Foo.class, Bar.class).alternatives(Bar.class).initialize()) {

Moreover, it's also possible to disable the discovery completely so that only the "synthetic" bean archive is considered:

WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bar.class).initialize()) {

In the same manner, it is possible to explicitly declare interceptors, decorators, extensions and Weld-specific options (such as relaxed construction) using the builder.

Weld builder = new Weld() 
.disableDiscovery() 
.packages(Main.class, Utils.class) 
.interceptors(TransactionalInterceptor.class) 
.property("org.jboss.weld.construction.relaxed", true); 
WeldContainer container = builder.initialize();

The builder is reusable which means that it's possible to initialize multiple Weld containers with one builder. However, note that containers must have a unique identifier assigned when running multiple Weld instances at the same time.
[中]该生成器是引导Weld SE容器的首选方法。
典型用法如下所示:

WeldContainer container = new Weld().initialize(); 
container.select(Foo.class).get(); 
container.event().select(Bar.class).fire(new Bar()); 
container.shutdown();

WeldContainer实现自动关闭:

try (WeldContainer container = new Weld().initialize()) { 
container.select(Foo.class).get(); 
}

默认情况下,将启用发现,以便考虑所有发现的bean存档中的所有bean。然而,可以分别定义一个“合成”bean归档,或一组bean类和启用:

WeldContainer container = new Weld().beanClasses(Foo.class, Bar.class).alternatives(Bar.class).initialize()) {

此外,还可以完全禁用发现,以便只考虑“合成”bean归档:

WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bar.class).initialize()) {

以同样的方式,可以使用生成器显式声明拦截器、装饰器、扩展和焊接特定选项(例如宽松构造)。

Weld builder = new Weld() 
.disableDiscovery() 
.packages(Main.class, Utils.class) 
.interceptors(TransactionalInterceptor.class) 
.property("org.jboss.weld.construction.relaxed", true); 
WeldContainer container = builder.initialize();

生成器是可重用的,这意味着可以用一个生成器初始化多个焊接容器。但是,请注意,当同时运行多个焊接实例时,容器必须指定唯一标识符。

代码示例

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws IOException {
  Weld weld = new Weld();
  WeldContainer container = weld.initialize();
  Application application = container.instance().select(Application.class).get();
  application.run();
  weld.shutdown();
}

代码示例来源:origin: cucumber/cucumber-jvm

protected void start(Weld weld) {
  try {
    if (weld == null) {
      weld = new Weld();
    }
    containerInstance = weld.initialize();
  } catch (IllegalArgumentException e) {
    throw new CucumberException(START_EXCEPTION_MESSAGE, e);
  }
}

代码示例来源:origin: org.drools/drools-cdi

@BeforeClass
public static void beforeClass() {    
  CDITestRunner.setUp();  
  
  CDITestRunner.weld = CDITestRunner.createWeld( CDIExamplesTest.class.getName(),
                          Msg.class.getName(), Msg1.class.getName(), Msg2.class.getName(), 
                          Message.class.getName(), MessageImpl.class.getName(), 
                          Message2.class.getName(), Message2Impl1.class.getName(), Message2Impl2.class.getName(),
                          MessageProducers.class.getName(), MessageProducers2.class.getName() );
  CDITestRunner.container = CDITestRunner.weld.initialize();
}

代码示例来源:origin: salaboy/jBPM5-Developer-Guide

private ExecutorModule() {
  weld = new Weld();
  this.container = weld.initialize();
  
  this.executorService = this.container.instance().select(ExecutorServiceEntryPointImpl.class).get();
  //Singleton.. that we need to instantiate
  //this.container.instance().select(TaskLifeCycleEventListener.class).get(); 
}

代码示例来源:origin: org.wamblee/wamblee-test-enterprise

/**
 * Must be called to initialize the bean manager. 
 */
public void initialize() {
  weld = new Weld();
  container = weld.initialize();
  beanManager = container.getBeanManager();
}

代码示例来源:origin: io.squark.yggdrasil.yggdrasil-framework-provider/yggdrasil-cdi-provider

@Override
public void provide(YggdrasilConfiguration configuration) throws YggdrasilException {
  logger.info("Initializing Weld container...");
  System.setProperty("org.jboss.logging.provider", "slf4j");
  Weld weld = new Weld();
  weld.property(Weld.ARCHIVE_ISOLATION_SYSTEM_PROPERTY, false);
  weld.setClassLoader(CDIProvider.class.getClassLoader());
  WeldContainer container = weld.initialize();
  YggdrasilContext.registerObject(BeanManager.class.getName(), container.getBeanManager());
  logger.info(CDIProvider.class.getSimpleName() + " initialized.");
}

代码示例来源:origin: io.silverware/cdi-microservice-provider

log.info("Hello from CDI microservice provider!");
final String weldName = String.valueOf(context.getProperties().get(Context.WELD_NAME));
final Weld weld = new Weld(weldName);
final MicroservicesCDIExtension microservicesCDIExtension = new MicroservicesCDIExtension(this.context);
weld.property("org.jboss.weld.se.archive.isolation", "false");
weld.addExtension(microservicesCDIExtension);
final WeldContainer container = weld.initialize();
this.context.getProperties().put(BEAN_MANAGER, container.getBeanManager());
this.context.getProperties().put(CDI_CONTAINER, container);
this.context.getProperties().put(Storage.STORAGE, new HashMap<String, Object>());
this.beanManager = container.getBeanManager();
this.deployed = true;
container.event().select(MicroservicesInitEvent.class).fire(new MicroservicesInitEvent(this.context, container.getBeanManager(), container));
container.event().select(MicroservicesStartedEvent.class).fire(new MicroservicesStartedEvent(this.context, container.getBeanManager(), container));
 this.deployed = false;
 try {
   weld.shutdown();
 } catch (final IllegalStateException e) {

代码示例来源:origin: stackoverflow.com

Weld weld = new Weld();
try {
  WeldContainer container = weld.initialize();

  URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
  ResourceConfig config = ResourceConfig.forApplicationClass(MyApplication.class);

  Server server = JettyHttpContainerFactory.createServer(baseUri, config);

  server.join();

} catch (Exception e) {
  e.printStackTrace();
} finally {
  weld.shutdown();
}

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

@Test
public void testConsumers() throws InterruptedException {
  try (WeldContainer weld = new Weld().disableDiscovery().addExtension(new VertxExtension()).addPackage(false, RegisterConsumersAfterBootstrapTest.class)
      .initialize()) {
    Vertx vertx = Vertx.vertx();
    try {
      weld.select(VertxExtension.class).get().registerConsumers(vertx, weld.event());
      vertx.eventBus().send(HelloObserver.HELLO_ADDRESS, "hello");
      assertEquals("hello", SYNCHRONIZER.poll(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
    } finally {
      vertx.close();
    }
  }
}

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

@Override
  public void run() {
    server.shutdownNow();
    weld.shutdown();
  }
}));

代码示例来源:origin: org.kie.workbench.screens/kie-wb-common-data-modeller-backend

@Before
public void setUp() throws Exception {
  // disable git and ssh daemons as they are not needed for the tests
  System.setProperty("org.uberfire.nio.git.daemon.enabled", "false");
  System.setProperty("org.uberfire.nio.git.ssh.enabled", "false");
  System.setProperty("org.uberfire.sys.repo.monitor.disabled", "true");
  //Bootstrap WELD container
  weldContainer = new Weld().initialize();
  dataModelService = weldContainer.select(DataModelerService.class).get();
  moduleService = weldContainer.select(KieModuleService.class).get();
  //Ensure URLs use the default:// scheme
  fs.forceAsDefault();
}

代码示例来源:origin: org.apache.camel/camel-test-cdi

CamelCdiDeployment(TestClass test, CamelCdiContext context) {
  this.context = context;
  weld = new Weld()
    // TODO: check parallel execution
    .containerId("camel-context-cdi")
    .property(ConfigurationKey.RELAXED_CONSTRUCTION.get(), true)
    .property(Weld.SHUTDOWN_HOOK_SYSTEM_PROPERTY, false)
    .enableDiscovery()
    .beanClasses(test.getJavaClass().getDeclaredClasses())
    .addBeanClass(test.getJavaClass())
    .addExtension(new CdiCamelExtension());
  // Apply deployment customization provided by the @Beans annotation
  // if present on the test class
  if (test.getJavaClass().isAnnotationPresent(Beans.class)) {
    Beans beans = test.getJavaClass().getAnnotation(Beans.class);
    weld.addExtension(new CamelCdiTestExtension(beans));
    for (Class<?> alternative : beans.alternatives()) {
      // It is not necessary to add the alternative class with WELD-2218
      // anymore, though it's kept for previous versions
      weld.addBeanClass(alternative)
        .addAlternative(alternative);
    }
    for (Class<?> clazz : beans.classes()) {
      weld.addBeanClass(clazz);
    }
    weld.addPackages(false, beans.packages());
  }
}

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

@Override
public void start(Future<Void> startFuture) throws Exception {
  Weld weld = this.weld != null ? this.weld : createDefaultWeld();
  if (weld.getContainerId() == null) {
    weld.containerId(deploymentID());
  }
  weld.addExtension(new VertxExtension(vertx, context));
  configureWeld(weld);
  // Bootstrap can take some time to complete
  vertx.executeBlocking(future -> {
    try {
      this.weldContainer = weld.initialize();
      future.complete();
    } catch (Exception e) {
      future.fail(e);
    }
  }, result -> {
    if (result.succeeded()) {
      LOGGER.info("Weld verticle started for deployment {0}", deploymentID());
      startFuture.complete();
    } else {
      startFuture.fail(result.cause());
    }
  });
}

代码示例来源:origin: org.apache.camel/camel-test-cdi

@Override
  public void evaluate() throws Throwable {
    WeldContainer container = weld.initialize();
    context.setBeanManager(container.getBeanManager());
    try {
      base.evaluate();
    } finally {
      container.shutdown();
      context.unsetBeanManager();
    }
  }
};

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

/**
 * The returned {@link Weld} instance has:
 * <ul>
 * <li>automatic discovery disabled</li>
 * <li>concurrent deployment disabled</li>
 * </ul>
 *
 * @return a new {@link Weld} instance suitable for testing
 */
public static Weld createWeld() {
  return new Weld().disableDiscovery().property(ConfigurationKey.CONCURRENT_DEPLOYMENT.get(), false);
}

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

/**
 *
 * @return a default {@link Weld} builder used to configure the Weld container
 */
public static Weld createDefaultWeld() {
  return new Weld().property(ConfigurationKey.CONCURRENT_DEPLOYMENT.get(), false);
}

代码示例来源:origin: stackoverflow.com

public class WebServiceBinder extends AbstractBinder {

 @Override
 protected void configure() {
  BeanManager bm = getBeanManager();
  bind(getBean(bm, StudentRepository.class))
    .to(StudentRepository.class);
 }

 private BeanManager getBeanManager() {
  // is there a better way to get the bean manager?
  return new Weld().getBeanManager();
 }

 private <T> T getBean(BeanManager bm, Class<T> clazz) {
  Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next();
  CreationalContext<T> ctx = bm.createCreationalContext(bean);
  return (T) bm.getReference(bean, clazz, ctx); 
 }
}

代码示例来源:origin: org.drools/drools-cdi

list.add(ResourceFactoryServiceImpl.class.getName());
Weld weld = new Weld();
List<Class<?>> classList = new ArrayList<>();
for (String className : list) {
weld.beanClasses(classList.toArray(new Class[0]));
weld.addExtension(new KieCDIExtension());
weld.disableDiscovery();
return weld;

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

@Override
public Weld addExtensions(Extension... extensions) {
  for (Extension extension : extensions) {
    addExtension(extension);
  }
  return this;
}

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

@Override
public Weld addProperty(String key, Object value) {
  property(key, value);
  return this;
}

相关文章