【问题标题】:Inject beans produced from a Websphere shared library into an EJB将 Websphere 共享库生成的 bean 注入 EJB
【发布时间】:2015-10-01 08:48:23
【问题描述】:

我有一个 EJB 会话 bean,它注入 Logger 从一个打包在一个 jar 文件中的类产生。 jar 文件被添加到类路径中。

package org.cdi.inject.test;

import javax.ejb.Stateless;
import javax.inject.Inject;

import org.apache.log4j.Logger;

@Stateless
public class MySessionBean{

  @Inject
  private Logger log;

}

产生 Logger 的类如下所示:

package org.cdi.inject.producer;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Singleton;

import org.apache.log4j.Logger;

@Singleton
public class LogProducer {

    @Produces
    public Logger getLogger(InjectionPoint ip){
        String declaringClass = ip.getMember().getDeclaringClass().getName();   
        return Logger.getLogger(declaringClass);
    }
}

MySessionBean 类被打包在 EJB jar 文件 MyEjb.jar 中,LogProducer 类被打包在 bean-producer.jar 中。正如here 所提到的,这两个jar 都包含一个META-INF 目录,其中包含beans.xml

我使用的服务器是Websphere 8.0。我已经直接通过控制台部署了 MyEjb.jar,并将 bean-producer.jar 添加到了shared library。共享库被添加到 ejb jar 的类路径中。

使用上述配置,注入失败并出现错误:

[10/1/15 12:56:53:762 GMT+05:30] 00000037 InjectInjecti E   CWOWB0102E: A JCDI error has occurred: Api type [org.apache.log4j.Logger] is not found with the qualifiers
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into
 Field Injection Point, field :  private org.apache.log4j.Logger org.cdi.inject.producer.MySessionBean.log, Bean Owner : [1527619878,Name:null,WebBeans Type:MANAGED,API Types:[org.cdi.inject.producer.MySessionBean,java.lang.Object],Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]]
         InjectionType   :  [class org.apache.log4j.Logger]
         Annotated       :  [Annotated Field,Base Type : class org.apache.log4j.Logger,Type Closures : [interface org.apache.log4j.spi.AppenderAttachable, class org.apache.log4j.Logger, class java.lang.Object, class org.apache.log4j.Category],Annotations : [@javax.inject.Inject()],Java Member Name : log]
         Qualifiers      :  [[@javax.enterprise.inject.Default()]]

at org.apache.webbeans.util.InjectionExceptionUtils.throwUnsatisfiedResolutionException(InjectionExceptionUtils.java:92)
... stacktrace truncated

但是,如果我将 LogProducer 添加到 MyEjb.jar,它会起作用。

【问题讨论】:

    标签: jakarta-ee ejb websphere cdi


    【解决方案1】:

    这是不可能的。 CDI 仅扫描打包在应用程序中的档案中的生产者注释(和托管 bean 类等),而不是共享库中的。这类似于注解@Stateless@WebServlet 的限制类必须封装在应用程序中。

    【讨论】:

    • 我有点想通了,所以我将 EJB 和生产者 jar 打包到同一个耳朵中并且注入工作。但我还有另一个问题,我收到了CWOWB0102E: A JCDI error has occurred: WebBeans context with scope type annotation @Singleton does not exist within current thread
    • @ares 我建议接受答案并为您的新问题创建一个新问题。我最好的猜测是您使用的是非 EE 线程,或者您在 WebSphere 中发现了一个错误。
    • 再想一想,拥有一个共享库的意义不是消除了将许多应用程序共享的 jar 打包到其特定 EAR 中的需要吗?
    • @ares 是的,这就是重点,但并非所有场景都支持它,尤其是那些 EE 规范要求类在 EAR 中的场景。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多