将 Websphere 共享库生成的 bean 注入到 EJB 中

Inject beans produced from a Websphere shared library into an EJB

我有一个 EJB 会话 bean,它注入 Logger 从 class 生成的 class ,它被打包在一个 jar 文件中。 jar 文件添加到 classpath.

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 的 class 如下所示:

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);
    }
}

ClassMySessionBean打包EJB jar文件MyEjb.jar和classLogProducer打包在 bean-producer.jar 中。如前所述 here,这两个 jar 都包含一个 META-INF 目录,其中包含 beans.xml

我使用的服务器是Websphere 8.0。我已经直接通过控制台部署了 MyEjb.jar,并且 bean-producer.jar 添加到 shared library。共享库被添加到 classejb 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,它会起作用。

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