Jakarta ee 将从Websphere共享库生成的bean注入EJB

Jakarta ee 将从Websphere共享库生成的bean注入EJB,jakarta-ee,ejb,websphere,cdi,Jakarta Ee,Ejb,Websphere,Cdi,我有一个EJB会话bean,它注入从打包在jar文件中的类生成的Logger。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; } 生

我有一个EJB会话bean,它注入从打包在jar文件中的类生成的
Logger
。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;

}
生成记录器的类如下所示:

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);
    }
}
Class
MySessionBean
打包在中,EJB jar文件MyEjb.jar和Class
LogProducer
打包在bean producer.jar中。如前所述,这两个JAR都包含一个
META-INF
目录,其中包含
beans.xml

我使用的服务器是
WebSphere8.0
。我已经通过控制台直接部署了MyEjb.jar,并且bean producer.jar被添加到
共享库中。共享库被添加到EJBJAR的类路径中

在上述配置中,注入失败并出现错误:

[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,它会工作。

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

我有点明白了,所以我将EJB和生产者jar打包到同一个ear中,注入工作正常。但我还有一个问题,我得到了
CWOWB0102E:发生了JCDI错误:当前线程中不存在范围类型注释为@Singleton的WebBeans上下文
@ares我建议接受答案并为新问题创建一个新问题。我的最佳猜测是您使用的是非EE线程,或者您在WebSphere中发现了一个bug。再想一想,拥有一个共享库的意义不就是消除了将许多应用程序共享的JAR打包到其特定EAR中的需要吗?@ares是的,这是重点,但并非所有场景都支持它,特别是那些EE规范要求类在EAR中的类。