【问题标题】:How to add Weld CDI support to a working Jetty application?如何将 Weld CDI 支持添加到工作中的 Jetty 应用程序?
【发布时间】:2015-12-16 20:35:08
【问题描述】:

我目前正在使用 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 =&gt; []

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

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

【问题讨论】:

  • 你在使用embedded-jetty吗? (无论是您自己的,还是来自 gradle?)还是您正在部署到 jetty-distribution?
  • @JoakimErdfelt 我正在部署到码头发行版(实际上将 .war 放入 $JETTY_HOME/webapps 并运行 java -jar start.jar)。

标签: java gradle jetty cdi weld


【解决方案1】:

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

这部分记录在 Weld/CDI 方面Environments: Jetty

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

${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>

【讨论】:

  • 感谢您的逐步解释!我试图遵循它,但现在我收到以下错误:2015-12-17 21:14:32.102:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet 再次跟随 java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE =&gt; [] 我尝试寻找包含 JettyJspServlet 类的 jar,但我找不到正确的依赖项www.mvnrepository.com。你知道在哪里可以找到它吗?
  • jsp 是一个不同的主题,请提出一个新的/不同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多