问题描述:
1、Spring启动流程中,获取beanfatory的时候会进行xml文件的读取,其中命名空间与xml校验使用了META-INF下的文件:
以beans举例:
理论上应该长上面那样。但是我打的jar包里面:
导出为可运行jar包时,多个spring jar包同时有,spring.handlers、spring.schemas、spring.tooling三个文
件,但是打成jar包时在META-INF/下只能留一份,这时需要将多个文件中的内容合并在一个文件中。
解决方式
使用maven打包方式。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>my-spring-app</finalName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.suirui.WebsocketServer</mainClass>
</transformer>
<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.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用maven-shade-plugin会使用默认的1.5版本编译。需要修改默认编译版本。
在${MAVEN_HOME}/conf/setting.xml中改变默认的编译版本,激活profile:
<profiles>
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<encoding>UTF-8</encoding>
</properties>
</profile>
</profiles>