main方法启动spring 打包成jar

时间:2022-05-20 09:35:13

1  

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cn.vsp.TestMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>

上述代码中在<mainClass></mainClass>之间是填写程序的入口类,即含main方法的类

2.编辑完上述插件后,再执行

mvn assembly:assembly

这是就会在target目录下生成   *.jar 文件

3.运行jar文件

java -jar *.jar

遇到的问题
1 会无法找到bean 其原因是未找到配置文件此时要在META-INF中添加Class-Path: .
2 此后遇到找不到相关空
META-INF的spring-handlers 添加确实属性,要一行一个。别跟系统的一样,具体值可到spring 的具体jar里面去找。
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandlerhttp\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandlerhttp\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandlerhttp\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandlerhttp\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandlerhttp\://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandlerhttp\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler
spring-schemas