如何向工作中的 Jetty 应用程序添加 Weld CDI 支持?

How to add Weld CDI support to a working Jetty application?

我目前正在使用 Jetty 9 部署我的 WAR 文件。

对于依赖管理,我使用Gradle;我的 build.gradle 文件如下所示:

apply plugin: 'java'
apply plugin: 'war'

repositories {
    mavenCentral()
}

dependencies {
    /**
     * Jersey.
     */
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.22.1'
    compile 'org.glassfish.jersey.core:jersey-server:2.22.1'
    /**
     * Weld CDI.
     */
    compile 'org.jboss.weld.servlet:weld-servlet-core:2.3.2.Final'

    /**
     * JUnit.
     */
    testCompile 'junit:junit:4.12'
}

我的 web.xml 看起来如下:

<web-app version="3.0">
<display-name>Example Api</display-name>
<servlet>
    <servlet-name>RESTful Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.example.api</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>RESTful Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
</web-app>

但是,当我尝试部署时,我得到以下堆栈跟踪:

[2015-12-16 09:13:33,315] Artifact Gradle : api : api.war: Artifact is being deployed, please wait...
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.EnhancedListener onStartup
INFO: WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
Dec 16, 2015 9:13:36 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.3.2 (Final)
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.WeldServletLifecycle initialize
INFO: WELD-ENV-000028: Weld initialization skipped - no bean archive found
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.Listener contextInitialized
INFO: WELD-ENV-001007: Initialize Weld using ServletContextListener
Dec 16, 2015 9:13:36 PM org.jboss.weld.environment.servlet.WeldServletLifecycle initialize
INFO: WELD-ENV-000028: Weld initialization skipped - no bean archive found

其次是 java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE => []

有人可以告诉我需要做什么才能让它正常工作吗?

我只是想 @Inject 我在 Jetty 中的依赖项,但我发现自己连续几天都在为配置设置而苦苦挣扎...

如果您想在 Jetty Distribution 上使用 Weld/CDI,您需要执行以下操作。

This is partly documented on the Weld/CDI side at Environments: Jetty

  1. 在 WAR 文件的 WEB-INF/lib 目录中包含适合您使用的 weld jar。 (我们称之为 mywebapp.war
  2. 创建一个${jetty.base}目录,并正确配置它。
  3. 请勿编辑/更改/删除/重命名 ${jetty.home}$JETTY_HOME 中的任何内容。如果你这样做,你就没有正确使用 jetty。
  4. 创建一个 XML 可部署文件以允许 Weld/CDI 使用名为 ${jetty.base}/webapps/mywebapp.xml
  5. 的文件查看标准 Servlet 类加载器隔离
  6. 复制mywebapp.war${jetty.base}/webapps/目录。
  7. 开始码头。

${jetty.base} 设置示例

$ mkdir /path/to/mybase
$ cd /path/to/mybase
$ java -jar /path/to/jetty-dist/start.jar --add-to-start=http,deploy

${jetty.base}/webapps/mywebapp.xml

的例子
<?xml version="1.0"?>

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/weld-numberguess</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/mywebapp.war</Set>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.server.handler.ContextHandler</Arg>
  </Call>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.servlet.FilterHolder</Arg>
  </Call>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.servlet.ServletContextHandler</Arg>
  </Call>
  <Call name="prependServerClass">
    <Arg>-org.eclipse.jetty.servlet.ServletHolder</Arg>
  </Call>
</Configure>