【问题标题】:Cant Inject Stateless Beans from other Projects无法从其他项目注入无状态 Bean
【发布时间】:2015-08-18 00:28:21
【问题描述】:

我遇到的问题是,当我尝试从打包在 jar 中的 Maven 项目 A 中注入 Bean 时,在我的主项目中。

带有控制器的主项目:

package com.swapIt.register.controller;

import java.io.Serializable;

import javax.inject.Inject;
import javax.inject.Named;

import com.svSwapIt.JPA.com.baustein.reg.ControllerHalloWorld;
import com.swapIt.register.Model.Person;
import javax.enterprise.context.SessionScoped;

@Named
@SessionScoped
public class ControllerRegistration implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Person person;
    @Inject
    private ControllerHalloWorld rm;;

    public Person getPerson(){
        if(this.person == null){
            this.person = new Person();
        }
        return this.person;
    }

    public void setPerson(Person person){
        this.person = person;
    }

        public String nav2Danke(){

        return "/contentFrame/registerDanke.xhtml";
    }

    public String getGreeting(){
        return rm.getGreeiting();
    }
}

项目A(在罐子里):

package com.svSwapIt.JPA.com.baustein.reg;
import javax.ejb.Stateless;

@Stateless
public class ControllerHalloWorld {

    public String getGreeiting() {
        return "Hallo World";
    }

}

我收到错误消息:

目标不可达,标识符“controllerRegistration”解析为空。

我知道它是因为 @Inject 注释。可以请人帮助我。我几乎尝试了一切。除了在主项目中添加依赖项之外,我还有什么特别需要考虑的吗?

我的 Beans XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">
</beans>

谢谢

【问题讨论】:

  • 您是否在项目 A 中添加了 bean.xml?你能发一下吗?哪个应用服务器?
  • 回答问题。我使用的是wildlfy 8.2.1。 Beans.xml 包含在两个项目的 META-INF 中。
  • 在上面发布我的 beans.xml。谢谢
  • 我对“目标无法访问,标识符 'controllerRegistration' 解析为空”有点困惑。错误。这是@Named bean,而不是hello world EJB 的名称。这个错误发生在哪里?在视图中?还是在部署时?
  • 它发生在视图上,当我打开注册表时......但是当我删除 ControllerRegistration 中的@inject 注释时它可以工作并且没有发生错误。

标签: java maven dependency-injection cdi java-ee-7


【解决方案1】:

在您的类 ControllerHalloWorld 中,您可以添加注解 @LocalBean 而在您的@inject 中,您可以使用@EJB Anotation。只是一个建议,我不确定它是否有效,但可以免费试用。 希望它有效。

【讨论】:

  • 我尝试在主项目中使用 ManagedBean 和 EJB Annotation 进行注入,并在 jar 项目中使用 LocalBean。现在我在部署过程中收到错误:JBAS018757: Error getting reflection information for class com.swapIt.register.controller.ControllerRegistration with ClassLoader ModuleClassLoader for Module "deployment.swapIt-app.war:main" from Service Module Loader
猜你喜欢
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
相关资源
最近更新 更多