错误:Unable to locate Spring NamespaceHandler for XML schema_裴大帅2020_新浪博客

blog.sina.com.cn · · 3209 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
一、错误背景:
       在java程序中加入了spring的成分,然后使用mvn clean package -Dmaven.test.skip=true进行打包,打包完毕后通过java -jar xx.jar执行程序,结果就报错了。更加详细的错误见如下:

[QC] WARN [main] org.springframework.context.support.AbstractApplicationContext.refresh(546) | Exception encountered during context initialization - cancelling refresh attempt: 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 [spring.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 [spring.xml]

源代码中加载spring.xml配置文件的代码是:
(1)main文件中:
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(SpringConfiguration.class);
applicationContext.refresh();

(2)SpringConfiguration文件中:
@Configuration
@EnableScheduling
@ComponentScan("com.test.algo.algo")
@EnableAsync
@Import({ MybatisConfig.class})
@ImportResource("classpath:spring.xml")
@PropertySource({"classpath:generator.properties"})
public class SpringConfiguration implements AsyncConfigurer {

assembly的一个bug.参见:http://jira.codehaus.org/browse/MASSEMBLY-360 原因是spring的多个jar包中都含有spring.handlers和spring.schemas文件,而assembly只会把第一次遇到的文件打入jar包,后面遇到的都会skip掉。

三、解决方法:
使用shade插件来打包.在shade的打包配制中指明spring.handlers和spring.schemas文件会以append方式加入进来,从而确保其他spring的jar中的这两个文件的信息不会被遗漏。加入下载3个标红的内容到pom文件中即可,见如下:
  1. <</span>plugin>  
  2.     <</span>groupId>org.apache.maven.plugins</</span>groupId>  
  3.     <</span>artifactId>maven-shade-plugin</</span>artifactId>  
  4.     <</span>version>1.4</</span>version>  
  5.     <</span>executions>  
  6.         <</span>execution>  
  7.             <</span>phase>package</</span>phase>  
  8.             <</span>goals>  
  9.                 <</span>goal>shade</</span>goal>  
  10.             </</span>goals>  
  11.             <</span>configuration>  
  12.                 <</span>transformers>  
  13.                     <</span>transformer  
  14.                         implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  15.                         <</span>mainClass>activiti.server.deploy.Server</</span>mainClass>  
  16.                     </</span>transformer>  
  17.                     <</span>transformer  
  18.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
  19.                         <</span>resource>META-INF/spring.handlers</</span>resource>  
  20.                     </</span>transformer>  
  21.                     <</span>transformer  
  22.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
  23.                         <</span>resource>META-INF/spring.schemas</</span>resource>  
  24.                     </</span>transformer>  
  25.                 </</span>transformers>  
  26.             </</span>configuration>  
  27.         </</span>execution>  
  28.     </</span>executions>  
  29. </</span>plugin>  
3209 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传