环境: Glassfish 3.1.2.2,JSF2,Weld 1.1.4.Final
我的EAR文件中有以下结构:
|-- lib
| `-- client-dummy-jar.jar
| |-- JarModuleBean.java
| `-- META-INF
| |-- MANIFEST.MF
| `-- beans.xml
|-- META-INF
| |-- MANIFEST.MF
| `-- application.xml
|-- client-dummy-ejb.jar
| |-- ClientEvent.java
| |-- Application.java
| |-- EjbModuleBean.java
| |-- META-INF
| | |-- MANIFEST.MF
| | `-- beans.xml
`-- client-dummy-web.war
|-- WEB-INF
| |-- web.xml
| `-- beans.xml
`-- index.xhtml
ejb和Web模块都对EAR / lib中的jar模块具有依赖性。
Application.java
@ApplicationScoped
@Named(value = Application.BEAN_NAME)
public class Application implements Serializable {
public static final String BEAN_NAME = "Application";
private static final long serialVersionUID = 2191033605110202697L;
@Inject
private Event<ClientEvent> clientEvent;
public void fireEvent() {
clientEvent.fire(new ClientEvent());
}
}
JarModuleBean.java / EjbModuleBean.java(除了类名/常量之外的所有内容都是相同的
@ApplicationScoped
@Named(value = JarModuleBean.BEAN_NAME)
public class JarModuleBean implements Serializable {
public static final String BEAN_NAME = "JarModuleBean";
private static final long serialVersionUID = -1583065083930316521L;
private boolean eventFired = false;
public String getEventString() {
if (eventFired) {
eventFired = false;
return "Event has been observed in " + BEAN_NAME;
} else {
return "No event has been observed in " + BEAN_NAME;
}
}
public void onClientEvent(@Observes ClientEvent event) {
eventFired = true;
}
}
的index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Client Dummy</title>
</h:head>
<h:body>
<h:form>
Event in JarModule: #{JarModuleBean.eventString}<br />
Event in EjbModule: #{EjbModuleBean.eventString}<br /><br />
<h:commandLink id="fireEvent" action="#{Application.fireEvent}">
<h:outputText value="Fire Event!"/>
</h:commandLink>
</h:form>
</h:body>
</html>
索引页的输出:
Event in JarModule:
Event in EjbModule: No event has been observed in EjbModuleBean
Fire Event!
触发事件后的输出:
Event in JarModule:
Event in EjbModule: Event has been observed in EjbModuleBean
Fire Event!
在我的application.xml
中,所有三个模块(ejb模块,Web模块和Java模块)都已注册。
从EjbModuleBean正确返回事件字符串。因此,似乎CDI甚至不扫描Glassfish 3.1.2.2上的EAR / lib目录中的库。根据Java EE 6规范(章节EE.8.2.1),EAR文件中的每个模块都应该可以看到EAR / lib文件夹中的库(或者在application.xml中指定为library-directory
的任何库)。
我错过了什么?
旁注:一切似乎都在7.1.1Final下工作,但我不确定哪个应用服务器是正确的(根据规范)或者我是否仍然做错了。