在Embedded Undertow / CDI / RestEasy中向注入上下文添加bean

时间:2017-08-24 01:43:26

标签: jax-rs cdi java-ee-7 weld undertow

给定带有CDI的嵌入式Undertow实例:

SomeObject myObject = new SomeObject();

UndertowJaxrsServer server = new UndertowJaxrsServer();
Undertow.Builder builder = Undertow.builder().addHttpListener(port, "0.0.0.0");
server.start(builder);

ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setApplication(new MyApplication(this));
deployment.setInjectorFactoryClass(CdiInjectorFactory.class.getName());

DeploymentInfo di = server.undertowDeployment(deployment, "/");

di.setClassLoader(getClass().getClassLoader())
.setContextPath("/")
.setDeploymentName("My deployment")
.addListeners(Servlets.listener(org.jboss.weld.environment.servlet.Listener.class));

server.deploy(di);

我希望能够在JAX-RS / CDI servlet上下文中注入我在容器外部创建的内容:

@Path
@Produces("application/json")
public class MyHandler {
  @Inject
  private SomeObject myObject;

  @Inject
  private UndertowJaxrsServer myServer;
}

如何将任意对象实例添加到CDI容器中?

注意:用例是:我需要在CDI上下文中注入创建Undertow服务器实例的类。我不能使用JVM单例,因为我可能需要旋转不止一个。

0 个答案:

没有答案