【问题标题】:How to integrate Weld to the maven project?如何将 Weld 集成到 maven 项目中?
【发布时间】:2023-03-29 18:04:02
【问题描述】:

试图导入这个项目(With Weld)但它不能

import org.jboss.weld.environment.se

在项目中使用 Weld 类。

https://github.com/agoncal/agoncal-book-javaee7

【问题讨论】:

  • 您是在尝试部署 webapp 还是 SE 应用程序?

标签: java dependency-injection cdi


【解决方案1】:

这实际上取决于您要达到的目标。 您是在编写 Java EE Web 应用程序(war/ear)还是独立应用程序? 另外:Weld 是 CDI 的参考实现,而 CDI 只是 Java EE 的一部分。如果您只使用 CDI(通过设置weld-se),则不能使用 EJB/EntityManager/Transactional/... ,它的“只是”CDI。

话虽如此:在 java 独立应用程序中使用weld-se 可以通过导入来完成

<dependency>
  <groupId>org.jboss.weld.se</groupId>
  <artifactId>weld-se</artifactId>
  <version>2.2.6.Final</version>
 </dependency>

引导应用程序“Main.class”通过

完成
    // Initialize Weld
    Weld theWeld = new Weld();
    WeldContainer theContainer = theWeld.initialize();


    // Execute the run method
    theContainer.instance().select(Main.class).get().run();


    // Shutting down Weld again
    theWeld.shutdown();

【讨论】:

    【解决方案2】:

    根据本书,您首先应该安装应用服务器来运行您的应用程序。在您的情况下,它将是 Glassfish。 here讨论了同样的问题。

    【讨论】:

      【解决方案3】:

      我还尝试在不做任何修改的情况下运行本书中的示例。 它不起作用的原因是 &lt;dependency&gt; 块中指定了不存在的 Weld 版本:

      <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se</artifactId>
        <version>2.0.0</version>
       </dependency>
      

      只需与可用版本列表进行比较,确保2.0.0 版本不在此处:http://repo1.maven.org/maven2/org/jboss/weld/se/weld-se/

      您可以使用任何其他版本,例如

      <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se</artifactId>
        <version>2.0.0.Final</version>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 2011-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-09
        相关资源
        最近更新 更多