将无状态 EJB 注入命名 Bean

Injecting Stateless EJB into Named Bean

学习教程后遇到问题。 系统:wildfly 10,多模块maven项目,打包:ear

EJB:

@Stateless
public class ToyService implements ToyServiceRemote, ToyServiceLocal {
...
}

接口:

@Local
public interface ToyServiceLocal {
...
}

豆类:

@Named("toyProducts")
@RequestScoped
public class ProductBean {

    @Inject
    private ToyServiceLocal toyService;

    @PostConstruct
    public void initialize() {
       toyList = toyService.getAllToys();
    }
    ...
}

JSF:

<ui:repeat value="#{toyProducts.toyList}" var="toy">
...
</ui:repeat>

应用程序已部署,但当我尝试在浏览器中打开页面时,我得到:

ERROR [io.undertow.request] (default task-62) UT005023: Exception handling request to /index.xhtml: javax.servlet.ServletException: Can not set com.example.common.service.ToyServiceLocal field shop.beans.ProductBean.toyService to com.example.product.service.ToyServiceLocal$ToyServiceRemote03029808$Proxy$_$$_Weld$EnterpriseProxy$ at javax.faces.webapp.FacesServlet.service(FacesServlet.java:671) wildfly-experiment | at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) wildfly-experiment | at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) wildfly-experiment | at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) ...

这次我找到了 "real" 答案:因为我使用的是多模块 maven 项目,打包为 EAR(带有一些 EJB、一个通用 JAR 和 WAR 文件)。我所要做的就是添加 WAR 文件中提供的范围(常见的是 EJB 中的 JAR 和产品)

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>common</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>product</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
</dependencies>