【发布时间】:2016-01-03 18:27:29
【问题描述】:
我正在尝试运行“Java EE 7 Development with WildFly”一书的示例。现在我面临以下问题/问题:
TheatreInfo.java:
@Model
public class TheatreInfo {
...
@Produces
@Named
public Collection<Seat> getSeats() {
return Lists.newArrayList(seats);
}
...
}
Seat.java:
@Dependent
@Named
public class Seat {
...
public String getName() {
return name;
}
...
}
index.xhtml:
<?xml version="1.0" encoding="UTF-8" ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
template="/WEB-INF/templates/default.xhtml">
<ui:define name="content">
<h1>TicketBooker Machine</h1>
<h:form id="reg">
<h:panelGrid columns="1" border="1" styleClass="smoke">
<h:dataTable var="_seat" value="#{seats}" rendered="#{not empty seats}" styleClass="simpletablestyle">
...
</h:dataTable>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
beans.xml:
<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"
version="1.1" bean-discovery-mode="all/annotated">
</beans>
这非常有效——我在我的网络浏览器中看到一张座位表——只要我在 beans.xml 中使用 bean-discovery-mode="all"。在 beans.xml 中使用 bean-discovery-mode="annotated" 后,我在浏览器中分别看不到座位表,我看到一个空表,但没有发生错误。
在书中他们使用 bean-discovery-mode="all" 但我更喜欢查看哪些类是托管 bean,哪些不是。要使用 bean-discovery-mode="annotated" 我必须将 @Dependent 添加到某些类,但我无法解决名称生产者方法的问题。有人可以帮忙吗?
【问题讨论】:
-
阅读此stackoverflow.com/questions/18310388/…,它可能对您有用。 “在隐式存档中,CDI 只能管理和注入使用范围类型注释的 bean。”来自docs.oracle.com/javaee/7/tutorial/cdi-adv001.htm#CACDCFDE
-
感谢您的这篇文章。我认为我的问题在于我的生产者方法的 Return-Type 是 Collection
我无法注释(第 3 方类),因此它不被视为可注入 bean!?