Glassfish 部署时EJB注入失败

Glassfish 部署时EJB注入失败,glassfish,ejb,code-injection,Glassfish,Ejb,Code Injection,我有一个exxh EJB的问题 首先,我的设置:我使用GlassFish&JEE6。我有一个打包为WAR的REST服务和一个打包为EJBJAR的bean。它们不在耳朵里。 EJB应该通过@EJB从REST-WAR使用,但是当我尝试部署WAR时,GlassFish显示了以下错误: 部署期间发生错误: Exception while deploying the app [exx-upload-1.0] : Cannot resolve reference Local ejb-ref name=com

我有一个exxh EJB的问题

首先,我的设置:我使用GlassFish&JEE6。我有一个打包为WAR的REST服务和一个打包为EJBJAR的bean。它们不在耳朵里。 EJB应该通过@EJB从REST-WAR使用,但是当我尝试部署WAR时,GlassFish显示了以下错误:

部署期间发生错误:

Exception while deploying the app [exx-upload-1.0] : Cannot resolve reference Local ejb-ref name=com.ex.exx.model.FileUpload/ocr,Local 3.x interface =com.ex.exx.api.IOCRService,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session. Please see server.log for more details.
(以前部署EJB时没有任何错误)

我不知道为什么。以下是EJB代码:

接口:

@Local
public interface IOCRService {
    public String performOCRonImage(BufferedImage input);
}
和执行:

@Stateless
@LocalBean
public class OCRScanner implements IOCRService  {

    private Logger logger = Logger.getLogger(this.getClass().getName());
    private final static String NOT_RECOGNIZED = "Can not regocnize text";
    /**
     * Default constructor.
     */
    public OCRScanner() {
        logger.log(Level.INFO, "### OCR SCANNER BUILD" + this);
    }



    public String performOCRonImage(BufferedImage input) {
        logger.log(Level.INFO, "### OCR SCANNER CALLED" + this);
    }

    ...
这是战争中的重要部分:

public class FileUpload {
private final File PROPERTIES_FILE = new File(
        "fileUploadProperties.properties");
private final String PARAMETER_NAME = "file";
private final Logger logger = Logger.getLogger(this.getClass().getName());


@EJB
private IOCRService ocr;

public Response uploadFile(...) {
    // do some stuff
    logger.log(Level.INFO, "### EJB" + ocr.toString())
}

奇怪的建议?我在这里找不到我的失败。

如果目标不是EJB,则不应使用
@EJB
。我猜这是你的情况,因为你试图在你的战争中注入一个类

而是使用:

@Inject
private IOCRService ocr;
基本上,
@Inject
在大多数情况下更好,因为:

  • 它更安全
  • 它支持@alternations
  • 它知道注入对象的范围

解决了这个问题,将@Local替换为@Remote。
但是,这是可行的,我不满意,因为我不明白为什么。

基本上,考虑到规范(如中所述),应用程序只能访问其他应用程序的EJB,如果它们用
@Remote
修饰

因此,您有3个选项:

  • @Remote
    (您所做的)装饰您的EJB
  • 将两者一起包装在耳朵内(因为它们将位于耳朵中) 相同的应用程序)。但是如果你想把它们分开部署 应用程序或独立服务器,请使用1。)
  • 将CDI与
    @Inject
    一起使用,但这仍然只会在以下情况下发现EJB 在同一应用程序中,如果不是,则装饰为
    @Remote
  • 嗯,,
    Alex

    另一个解决方案是添加
    @Stateless(name=”“)
    ,这个工作表单

    感谢您的回答,但它是一个EJB。用另一种方式解决了我的问题,稍后将发布回复