由於您使用的是您提到的技術堆棧,因此也可以使用CDI。無論如何,如果你將所有東西都打包成WAR,那只是運行時的一個模塊。假設你有以下結構:
--- WAR
|--- any *.xhtml files can live here (public)
|--- WEB-INF
|--- web.xml (optional)
|--- persistence.xml (optional) - declare persistence unit used from WEB-INF/classes
|--- beans.xml (optional) enable CDI annotation scanning for WEB-INF/classes
|--- faces-config.xml (optional) enable JSF annotation scanning in WEB-INF/classes
|--- *.xhtml files can live here (private - usually templates, ui:composition, etc.)
|--- classes
|--- com.example - put managed beans here, EJBs, JPA etities or just about any other *.class
|---lib
|--- jpa.jar
|--- META-INF
|--- persistence.xml - persistence unit used in this jar
|--- com.example - JPA entities can live here, gets mapped to WEB-INF/classes at runtime
|--- ejb.jar
|--- META-INF
|--- ejb-jar.xml (optional) but can declare resources here
|--- com.example - ejbs can live here, gets mapped to /WEB-INF/classes at runtime
|--- faces.jar
|--- META-INF
|--- faces-config.xml - enables scanning JSF specific annotations in this jar
|--- resources - this gets mapped to the WAR root at runtime, .xhtml can live here
|--- com.example - put your @ManagedBean s here and @EJB inject anything from ejb.jar, gets mapped to /WEB-INF/classes at runtime
|--- cdi.jar
|--- META-INF
|--- beans.xml - marks this as CDI bean archive
|--- com.example - CDI beans can live here, gets mapped to WEB-INF/classes at runtime
注意,即使我已經明確分開jpa.jar,ejb.jar,faces.jar和CDI.jar在上述結構中,沒有人說你有 - 你可以根據需要混合搭配。該點帶走:
- 一戰的
WEB-INF/lib
類中的任何JAR在運行時獲取映射到/WEB-INF/classes
beans.xml
使CDI
faces-config.xml
使JSF註釋掃描
ejb-jar.xml
不被任何必需的,但你可以利用它
web.xml
可以住在WEB-INF/
,但不是必需的
web-fragment.xml
可以在WEB-INF/lib
住在什麼罐子的META-INF
文件夾和被合併到web.xml
.xhtml
JSF文件可以在WEB-INF/
(私人)生活在Web應用程序根目錄(公開),並在裏面WEB-INF/lib
任何廣口瓶META-INF/resources
- 所有這些文件被映射到Web應用程序根目錄(即。Ë他們可以在邏輯裏面WEB-INF/
結束,如果在罐子裏他們是內META-INF/resource/WEB-INF
)
persistence.xml
可以住在WEB-INF
或內部的任何廣口瓶的META-INF
內WEB-INF/lib
這應該只是掩蓋它 - 讓我知道如果什麼都不清楚或者可以加入。
好吧,不知何故,我的Maven包裝奇怪!並非所有的bean都以WEB-INF/classes結尾。它們在我配置它們的單獨快照中。什麼錯誤的是,實現只在WEB-INF/classes中搜索...... – Tibbers
通過bean,你是指JSF管理的bean或CDI bean或EJB? – rdcrng
但它並不重要,它大致相同,只是不是所有的類都映射到WEB-INF/classes。這實際上是我的問題。感謝您的解釋,它有助於找到我的包裝問題。我已經用Maven修復了它們!無論如何,我會認爲這是正確的答案,因爲您提供有關如何打包bean以使其可訪問的見解! – Tibbers