最近项目要用到JMS,因为用tomcat容器,所以采用了开源的ActiveMQ  消息中间件提供JMS支持。但是在spring2.5和activemq5.2集成的时候出现了点问题,首先列出activemq-import-beans.xml的内容:
Java代码
<beans xmlns= "http://www.springframework.org/schema/beans"        
    xmlns:amq="http://activemq.org/config/1.0"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        
                                                                        http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq.xsd">    
 
运行测试程序出现以下异常:
Java代码
Caused by: org.xml.sax.SAXParseException: TargetNamespace.1: Expecting namespace 'http: //activemq.org/config/1.0', but the target namespace of the schema document is 'http://activemq.apache.org/schema/core'.        
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)        
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)        
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)        
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2525)        
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.constructTrees(XSDHandler.java:768)        
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:569)        
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552)        
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2408)        
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1753)        
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)        
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)        
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2740)        
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)        
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)        
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)        
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)        
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)        
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)        
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)        
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)        
        at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)        
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)        
        ... 22 more    
 
通过阅读异常信息发现时XSD名称空间不对,所以赶紧打开activemq-core-5.2.0.jar/META-INF/目录中的spring.schemas查看,发现"http\://activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd"
于是乎又打开activemq.xsd查个究竟,以下为activemq5.2的activemq.xsd文件一部分:
Java代码
<xs:schema elementFormDefault='qualified'    
                     targetNamespace='http: //activemq.apache.org/schema/core'    
                     xmlns:xs='http: //www.w3.org/2001/XMLSchema'    
                     xmlns:tns='http: //activemq.apache.org/schema/core'>    
 
噢,原来目标名称空间不一致啊。于是赶紧去改activemq-import-beans.xml文件的配置,修改如下:
Java代码
<?xml version= "1.0" encoding= "UTF-8"?>        
<beans xmlns= "http://www.springframework.org/schema/beans"        
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance            
xmlns:amq="http://activemq.apache.org/schema/core"    //这个地方很关键,amq对应的XSD名称空间名称要和activemq.xsd的一致        
xsi:schemaLocation="        
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd        
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq.xsd">
 
呵呵,到这里以为问题已经搞定,但是运行测试后,又发现一个新的问题,异常信息如下:
Java代码
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http: //activemq.apache.org/schema/core/activemq.xsd', because 1) could not find the document;        
                             2) the document could not be read;        
                             3) the root element of the document is not <xsd:schema>.    
查看了异常信息后,发现可能是spring无法定位到 http://activemq.apache.org/schema/core/activemq.xsd,于是又一次打开activemq-core-5.2.0.jar/META-INF的spring.schemas,内容如下:
Java代码
http\: //activemq.org/config/1.0=activemq.xsd        
http\: //activemq.org/config/1.0/1.0.xsd=activemq.xsd        
http\: //activemq.apache.org/schema/core=activemq.xsd        
http\: //activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd    
 
呵呵,发现问题了吗?初看没啥问题,但是仔细一看,噢,原来http\://activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd 与 http://activemq.apache.org/schema/core/activemq.xsd 不匹配,呵呵,知道问题根源了,现在时解决办法,解决办法有两种:
第一种:修改activemq-import-beans.xml文件的“ http://activemq.apache.org/schema/core/activemq.xsd ”这句为: http://activemq.apache.org/schema/core/activemq-core.xsd(这个就是activemq jar包中spring.schemas文件的最后一行,但是注意:http\://要改为http://)
 
第二种:在classpath目录中,新建META-INF/spring.schemas文件,然后增加一下内容:
Java代码
http\: //activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd    
 
这个时候重新运行测试,发现一切OK。哈。。