环境:JDK1.6+JBoss5.1.0+Struts2.1.8+Spring2.5.6+Hibernate3.3.2
- 将 war 包放入“ JBOSS 安装目录 /server/default/deploy ”目录,直接启动,报如下错误:
- 09 : 24 : 46 , 890 ERROR [JBossContextConfig] XML error parsing: context.xml
- org.jboss.xb.binding.JBossXBRuntimeException: Failed to create a new SAX parser
- at org.jboss.xb.binding.UnmarshallerFactory$UnmarshallerFactoryImpl.newUnmarshaller(UnmarshallerFactory.java:100 )
分析可能与 JBoss4.2.3 版本类似,存在 classloader 问题,参考 JBoss 社区资料( http://community.jboss.org/wiki/classloadingconfiguration ),设置 Web 应用加载时使用 JBoss 统一的 classloader 。具体操作方法为:在 WEB-INF 下增加 jboss-web.xml :- <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd" >
- < jboss-web >
- < class-loading java2ClassLoadingCompliance = "true" >
- < loader-repository >
- com.example:archive = unique -archive-name
- < loader-repository-config >
- java2ParentDelegation = true
- </ loader-repository-config >
- </ loader-repository >
- </ class-loading >
- </ jboss-web >
说明: com.example:archive=unique-archive-name 代表 jar 仓库的对象名 objectName ,其中 ,com.example 可以随意取, unique-archive-name 就用部署包的名字即可,例如 com.example:archive=demo.war ,保证这一串唯一即可。
详细说明可以通过 google 搜索“ JBoss 类隔离”。 - 重启 JBoss ,上述错误不再出现,但又报如下错误:
- 14 : 24 : 15 , 406 ERROR [ContextLoader] Context initialization failed
- org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure during classpath scanning; nested exception is java.io.FileNotFoundException: C:/JavaPro/jboss-5.1 . 0 .GA/server/ default /deploy/demo.war/WEB-INF/lib/demo.jar/com/demo (系统找不到指定的路径。)
参考 https://jira.springframework.org/browse/SPR-5120 ,下载 jboss-as-sprint-int-5.0.0.GA.jar 和 jboss-spring-int-vfs.jar ,拷贝至 Web 应用的 lib 下,修改 web.xml ,加入:
- <context-param>
- <param-name>contextClass</param-name>
- <param-value>org.jboss.spring.vfs.context.VFSXmlWebApplicationContext</param-value>
- </context-param>
- 重新启动,上述错误不再出现,但报如下错误:
- 14 : 42 : 49 , 484 ERROR [ContextLoader] Context initialization failed
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Invalid relative resource location [applicationContext-hibernate.xml] to import bean definitions from Offending resource: FileHandler @21170058 [path=demo.war/WEB-INF/classes/spring/applicationContext.xml context=file:/C:/JavaPro/jboss- 5.1 . 0 .GA/server/ default /deploy/ real=file:/C:/JavaPro/jboss- 5.1 . 0 .GA/server/ default /deploy/demo.war/WEB-INF/classes/spring/applicationContext.xml]; nested exception is java.io.IOException: Error listing files: C:/JavaPro/jboss- 5.1 . 0 .GA/server/ default /deploy/demo.war/WEB-INF/classes/spring/applicationContext.xml
- ............
- Caused by: java.io.IOException: Error listing files: C:/JavaPro/jboss-5.1 . 0 .GA/server/ default /deploy/demo.war/WEB-INF/classes/spring/applicationContext.xml
修改 spring 的配置中类似 <import resource="applicationContext-xxx.xml"/> 的内容,改成 <import resource="classpath:applicationContext-xxx.xml"/> 。我的 demo 中 spring 配置文件在 classes 的 spring 文件夹下,因此,应该改成: <import resource="classpath:spring/applicationContext-xxx.xml"/> - 重新启动,不再报任何错误,访问首页正确显示,并能登录成功。