`
chenzhou123520
  • 浏览: 4248010 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Unable to locate Spring NamespaceHandler for XML schema namespace

阅读更多

java服务中整合了spring,在Eclipse里本地启动时没问题,但是部署到局域网linux服务器上时解析spring applicationContext.xml报错,具体报错信息如下:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]

 

关于这个问题,纠结了我大半天时间,网上的资料也是众说纷纭,有的说是spring版本不统一、有些说是缺少对应的spring依赖的。不过这些原因都被我逐一排除了。

 

功夫不负有心人,在仔细地对比和排查原因之后,发现了问题的所在,在我的jar包下的META-INF目录下,有两个跟spring相关的文件:spring.handlers、spring.schemas,打开这两个文件一看,里面都只包含了spring-tx的配置

spring.handlers:

http\://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler

spring.schemas:

http\://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
http\://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-3.1.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd

里面并没有spring-context的schema和handler配置,所以会报错。

 

那么问题的根源是什么呢,在stackoverflow上找到了答案:http://stackoverflow.com/questions/1937767/spring-3-0-unable-to-locate-spring-namespacehandler-for-xml-schema-namespace

 

因为我使用了maven-shade-plugin这个maven打包插件,主要原因是插件配置不当导致,我原来的配置如下:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version> 1.7.1</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
				<transformers>
					<transformer
						implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
						<mainClass>com.chenzhou.test.Main</mainClass>
					</transformer>
				</transformers>
			</configuration>
		</execution>
	</executions>
</plugin>

由于没有配置META-INF/spring.handlers和META-INF/spring.schemas所以如果工程中依赖了Spring的多个依赖,在打包时后面的会把前面的覆盖,使得这两个文件中永远只保存最后一个spring依赖的schema和handler。

解决方法就是在里面加上META-INF/spring.handlers和META-INF/spring.schemas的配置:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version> 1.7.1</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
				<transformers>
					<transformer
						implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
						<resource>META-INF/spring.handlers</resource>
					</transformer>
					<transformer
						implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
						<resource>META-INF/spring.schemas</resource>
					</transformer>
					<transformer
						implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
						<mainClass>com.chenzhou.test.Main</mainClass>
					</transformer>
				</transformers>
			</configuration>
		</execution>
	</executions>
</plugin>

和第一段配置对比,主要增加了:

<transformer
	implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
	<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
	implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
	<resource>META-INF/spring.schemas</resource>
</transformer>

这段配置意思是把spring.handlers和spring.schemas文件以append方式加入到构建的jar包中。

 

修改完后,再次打包,此时就会把工程依赖的所有的spring依赖的schema和handler都加载到spring.handlers和spring.schemas里面。加载applicationContext.xml时就不会报错了。

 

关于maven-shade-plugin的使用,可以参考我另外一篇博文:使用maven插件对java工程进行打包

分享到:
评论
8 楼 promisepk 2017-06-28  
虽然已经找到 原因了,但是楼主写的还是很清楚 的
7 楼 南蚁北漂 2016-07-02  
不错,学习了!http://cobaya.cn/
6 楼 羽风之扬 2016-05-31  
你好,我有个问题向你请教下,发你邮箱了,麻烦看下,谢谢!
5 楼 Atk 2014-11-17  
非常感谢,我也是这个问题,解决了。
4 楼 t2xingzhe 2014-06-09  
这个一定要感谢下,帮我大忙了
3 楼 chenzhou123520 2014-04-02  
eddie_520 写道
没好使啊,还是那个错误!

我现在自己和公司都是用的这种方式打包的,是没问题的啊,不知道是不是你没配置好还是什么原因,如果不涉及到机密的话可以把工程发给我我给你测试一下: chenzhou1025@126.com
2 楼 eddie_520 2014-03-21  
我记得我给你留言了?删了?
1 楼 eddie_520 2014-03-21  
没好使啊,还是那个错误!

相关推荐

Global site tag (gtag.js) - Google Analytics