Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
本来比较习惯直接用eclipse里面的export成可运行jar包,开始选择的library handling 是第一个选项,结果运行后一直报下面的错误;
java -jar testmq.jar
六月 18, 2018 7:11:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17a7cec2: startup date [Mon Jun 18 19:11:16 CST 2018]; root of context hierarchy
六月 18, 2018 7:11:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [Producer.xml]
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [Producer.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:301)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1408)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1401)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:172)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.autohome.mq.testmq.ProducerMain.main(ProducerMain.java:16)
查询了好多同样的问题,以为是直接export不太对,需要用maven打包,比如看到了下面这个链接https://www.cnblogs.com/icewee/articles/3703491.html,是因为缺什么包导致;
然后换成了mavn打包,结果还是不太对,和我用export导出来的效果一样,查了很久都没有查到原因;
刚开始,用mvn打包,直接都是打成文件的
https://www.cnblogs.com/dzblog/p/6913809.html
按这个方式去打包,结果都生成了别的lib包,没有直接打进jar包;所以只能在当前目录下运行;
然后看到了这篇文章 https://blog.csdn.net/birdben/article/details/51951651
明白了原因,是因为因为的plugin不对;
主要使用了maven-jar-plugin和maven-dependency-plugin这两个Maven的插件,maven-jar-plugin负责构建jar包,maven-dependency-plugin负责导出所有依赖的jar包,这里我们使用了maven-assembly-plugin插件,这个插件可以将所有当前jar依赖的所有jar包打包到当前jar里面,这样就不需要导出所有依赖的jar包了,直接运行jar就可以了,实在太方便了 ^_^
但是这样还是不行,感觉和export的效果其实是一致的;于是又找了一些文章
https://blog.csdn.net/qing0706/article/details/51612040
,总算找到了原因;结果也是确实是因为
spring.handlers、spring.schemas,打开这两个文件一看,里面都只包含了spring-mvc的配置
需要在pom的打包后面添加别的插件,
再一想,我用maven和eclipse打包效果一样,是不是eclipse本身就有这样的方法了,
于是,尝试了一下,过然如此;
来源于 https://www.cnblogs.com/jiangwangxiang/p/8596785.html 中间部分