一、错误背景:
在java程序中加入了spring的成分,然后使用mvn clean package
-Dmaven.test.skip=true进行打包,打包完毕后通过java -jar
xx.jar执行程序,结果就报错了。更加详细的错误见如下:
[QC] WARN [main]
org.springframework.context.support.AbstractApplicationConte xt.refresh(546)
| Exception encountered during context initialization - cancelling
refresh attempt:
org.springframework.beans.factory.parsing.BeanDefinitionParsingExc eption:
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.BeanDefinitionParsingExc eption:
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文件中:
AnnotationConfigApplicat ionContext applicationContext = new
AnnotationConfigApplicat ionContext();
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文件中即可,见如下:
-
<</span>plugin>
-
<</span>groupId>org.apache.maven.plugins</</span>groupId> -
<</span>artifactId>maven-shade-plugin</</span>artifactId> -
<</span>version>1.4</</span>version> -
<</span>executions> -
<</span>execution> -
<</span>phase>package</</span>phase> -
<</span>goals> -
<</span>goal>shade</</span>goal> -
</</span>goals> -
<</span>configuration> -
<</span>transformers> -
<</span>transformer -
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransfor mer"> -
<</span>mainClass>activiti.server.deploy.Server</</span>mainClass> -
</</span>transformer> -
<</span>transformer -
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> -
<</span>resource>META-INF/spring.handlers</</span>resource> -
</</span>transformer> -
<</span>transformer -
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> -
<</span>resource>META-INF/spring.schemas</</span>resource> -
</</span>transformer> -
</</span>transformers> -
</</span>configuration> -
</</span>execution> -
</</span>executions> -
</</span>plugin>
本文来自:blog.sina.com.cn
感谢作者:blog.sina.com.cn
查看原文:错误:Unable to locate Spring NamespaceHandler for XML schema_裴大帅2020_新浪博客