spring2.5 activemq5.2

本文记录了Spring2.5与ActiveMQ5.2整合过程中遇到的名称空间不匹配问题及解决方案,包括调整XML配置文件中的名称空间和确保XSD文件路径正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring2.5,Activemq5.2整合遇到的问题以及解决方法

关键字: activemq, spring

最近项目要用到JMS,因为用tomcat容器,所以采用了开源的ActiveMQ  消息中间件提供JMS支持。但是在spring2.5和activemq5.2集成的时候出现了点问题,首先列出activemq-import-beans.xml的内容:

Java代码 复制代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"    
  2.   xmlns:amq="http://activemq.org/config/1.0"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  5.                                     http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq.xsd">  
<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代码 复制代码
  1. 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'.   
  2.     at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)   
  3.     at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)   
  4.     at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)   
  5.     at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2525)   
  6.     at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.constructTrees(XSDHandler.java:768)   
  7.     at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:569)   
  8.     at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552)   
  9.     at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2408)   
  10.     at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1753)   
  11.     at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)   
  12.     at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)   
  13.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2740)   
  14.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)   
  15.     at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)   
  16.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)   
  17.     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)   
  18.     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)   
  19.     at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)   
  20.     at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)   
  21.     at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)   
  22.     at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)   
  23.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)   
  24.     ... 22 more  
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代码 复制代码
  1. <xs:schema elementFormDefault='qualified'  
  2.            targetNamespace='http://activemq.apache.org/schema/core'  
  3.            xmlns:xs='http://www.w3.org/2001/XMLSchema'  
  4.            xmlns:tns='http://activemq.apache.org/schema/core'>  
<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代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3. xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance     
  4. xmlns:amq="http://activemq.apache.org/schema/core"  //这个地方很关键,amq对应的XSD名称空间名称要和activemq.xsd的一致   
  5. xsi:schemaLocation="    
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  7. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq.xsd">   
<?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代码 复制代码
  1. 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.                2) the document could not be read;    
  3.                3) the root element of the document is not <xsd:schema>.  
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代码 复制代码
  1. http/://activemq.org/config/1.0=activemq.xsd   
  2. http/://activemq.org/config/1.0/1.0.xsd=activemq.xsd   
  3. http/://activemq.apache.org/schema/core=activemq.xsd   
  4. http/://activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd  
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代码 复制代码
  1. http/://activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd  
http/://activemq.apache.org/schema/core/activemq-core.xsd=activemq.xsd

 这个时候重新运行测试,发现一切OK。哈。。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值