Spring导出可以运行的jar包问题如何解决

其他教程   发布日期:2023年07月22日   浏览次数:425

这篇文章主要介绍“Spring导出可以运行的jar包问题如何解决”,在日常操作中,相信很多人在Spring导出可以运行的jar包问题如何解决问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring导出可以运行的jar包问题如何解决”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

最近需要解决Maven项目导入可执行的jar包的问题,如果项目不包含Spring,那么使用mvn assembly:assembly即可

可是如果包含Spring,那么这么方法就不可行,报错:

  1. Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace

我在网上折腾了两天,这是assembly的一个bug。

据说原因是spring的多个jar包中都含有spring.handlers和spring.schemas文件,而assembly只会把第一次遇到的文件打入jar包,后面遇到的都会skip掉。

解决方法就是放弃assembly,使用shade插件来打包.在shade的打包配制中指明spring.handlers和spring.schemas文件会以append方式加入进来,从而确保其他spring的jar中的这两个文件的信息不会被遗漏。

下面是一个非常简单的例子,只有四个文件的Maven工程,代码再附件内:

1、pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.javaee</groupId>
  5. <artifactId>main-spring</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>3.0.6.RELEASE</version>
  12. </dependency>
  13. </dependencies>
  14. <build>
  15. <plugins>
  16. <plugin>
  17. <groupId>org.apache.maven.plugins</groupId>
  18. <artifactId>maven-shade-plugin</artifactId>
  19. <version>1.7</version>
  20. <executions>
  21. <execution>
  22. <phase>package</phase>
  23. <goals>
  24. <goal>shade</goal>
  25. </goals>
  26. <configuration>
  27. <finalName>my-spring-app</finalName>
  28. <shadedArtifactAttached>true</shadedArtifactAttached>
  29. <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
  30. <transformers>
  31. <transformer
  32. implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  33. <mainClass>com.test.Main</mainClass>
  34. </transformer>
  35. <transformer
  36. implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  37. <resource>META-INF/spring.handlers</resource>
  38. </transformer>
  39. <transformer
  40. implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  41. <resource>META-INF/spring.schemas</resource>
  42. </transformer>
  43. <transformer
  44. implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  45. <resource>META-INF/spring.tooling</resource>
  46. </transformer>
  47. </transformers>
  48. </configuration>
  49. </execution>
  50. </executions>
  51. </plugin>
  52. </plugins>
  53. </build>
  54. </project>

2、applicationContext.xml(Spring的配置文件)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  8. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. ">
  11. <context:annotation-config />
  12. <context:component-scan base-package="com.*" />
  13. </beans>

3、代码事例

  1. package com.service;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class UserService {
  5. public String findUser() {
  6. return "find liqiu";
  7. }
  8. }
  1. package com.exec;
  2. import org.springframework.context.support.GenericXmlApplicationContext;
  3. import com.service.UserService;
  4. public class Main {
  5. public static void main(String[] args) throws InterruptedException {
  6. GenericXmlApplicationContext context = new GenericXmlApplicationContext();
  7. context.setValidating(false);
  8. context.load("classpath:applicationContext.xml");
  9. context.refresh();
  10. UserService userService = context.getBean(UserService.class);
  11. while (true) {
  12. System.out.println(userService.findUser());
  13. Thread.sleep(10000);
  14. }
  15. }
  16. }

在命令行运行:mvn package

然后在target目录会产出一个jar包:my-spring-app.jar

运行即可:java -jar target/my-spring-app.jar

  1. 五月 16, 2015 10:46:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  2. INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
  3. 五月 16, 2015 10:46:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  4. INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@4a5e88f7: startup date [Sat May 16 22:46:53 CST 2015]; root of context hierarchy
  5. 五月 16, 2015 10:46:53 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  6. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18fd54ec: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userService]; root of factory hierarchy
  7. find liqiu
  8. find liqiu

当然也可以这么运行:java -classpath target/my-spring-app.jar com.exec.Main

以上就是Spring导出可以运行的jar包问题如何解决的详细内容,更多关于Spring导出可以运行的jar包问题如何解决的资料请关注九品源码其它相关文章!