在JBoss的文檔中,宣稱JBoss4.0版本是業界第一個支持J2EE1.4規范的應用服務器。以前公司的很多程序都是在JBoss3.2.x的版本上開發的,所以就想把他們移植到JBoss4.0.2中。于是,我就把預案原來程序原封不動的copy到JBoss4.0.2的default/deploy目錄中,但是運行時卻怎么也出不來結果。最后通過一步步跟蹤,發現程序在獲得EJB的home接口時發生了異常,異常類型為ClassCastException——類型轉換錯誤。 在JBoss的服務器類型中一般會包含三種配置:default、minimal和all,這種配置方式在3.2和4.0.1+中都是一致,但是有一個版本例外,那就是4.0.0。在4.0.0中包含了4種服務器配置:default、minimal、all和standard。在4.0.0中standard配置就相當于3.2和4.0.1+中的default配置,而4.0.0中的defaut配置和standard配置是一樣的,除了default完全兼容于J2EE1.4規范。在3.2和4.0.1+的default配置中以及4.0.0的standard配置中,JBoss在它的統一標準的類裝載器里啟動所有的J2EE服務。這樣,當所有組件都發布在同一個JVM中時,它就會獲得最優化的性能。但是,在JBoss的這種配置中,被發布的應用程序并沒有被嚴格劃分。于是就導致這種配置并不能完全適應J2EE1.4的規范。因此當我們把程序直接copy過來時才會發生異常。為了使這種配置完全適應J2EE規范,我們必須按照以下的步驟修改配置文件的設置,使范圍類裝載行為和通過值調用的JNDI查找行為起作用。 第一步,編輯conf/jboss-services.xml文件,將NamingService的CallByValue屬性的值設置為true。 <mbean code="org.jboss.naming.NamingService" name="jboss:service=Naming"> <!-- The call by value mode. true if all lookups are unmarshalled using the caller's TCL, false if in VM lookups return the value by reference. --> <attribute name="CallByValue">true</attribute> ... </mbean> 第二步,編輯deploy/ear-deployer.xml文件,設置Isolated和CallByValue屬性的值為true。 <server> <!-- EAR deployer, remove if you are not using ear deployments --> <mbean code="org.jboss.deployment.EARDeployer" name="jboss.j2ee:service=EARDeployer"> <!-- A flag indicating if ear deployments should have their own scoped class loader to isolate their classes from other deployments. --> <attribute name="Isolated">true</attribute> <!-- A flag indicating if the ear components should have in VM call optimization disabled. --> <attribute name="CallByValue">true</attribute> </mbean> </server> 最后,編輯deploy/jbossweb-tomcat55.sar/META-INF/jboss-service.xml文件,設置java2ClassLoadingCompliance和UseJBossWebLoader屬性的值為true。 <server> <mbean code="org.jboss.web.tomcat.tc5.Tomcat5" name="jboss.web:service=WebServer"> <!-- Get the flag indicating if the normal Java2 parent first class loading model should be used over the servlet 2.3 web container first model. --> <attribute name="Java2ClassLoadingCompliance">true</attribute> <attribute name="LenientEjbLink">true</attribute> <!-- A flag indicating if the JBoss Loader should be used. This loader uses a unified class loader as the class loader rather than the tomcat specific class loader. --> <attribute name="UseJBossWebLoader">true</attribute> 經過以上步驟地修改以后,這個配置就完全適應J2EE規范了,升級時出現的問題也就順利解決了。