spring Boot项目:如何选择多个启动类中的一个?
在spring boot开发中,有时项目包含多个启动类,例如主应用和独立模块。打包成单个JAR后,如何指定启动哪个类呢?本文将解答这个问题。
问题:Spring Boot项目包含两个或多个启动类,打包成可执行JAR后,如何选择并启动其中一个?
解决方案:利用Spring Boot maven插件(spring-boot-maven-plugin)的mainClass参数。该插件负责将Spring Boot应用打包成可执行JAR。通过配置mainClass属性,指定程序启动时使用的主类。
操作:在pom.xml文件中,找到spring-boot-maven-plugin插件配置,添加mainClass属性,其值为目标启动类的全限定名。例如,两个启动类分别为com.example.MainApp和com.example.ModuleApp,要启动com.example.ModuleApp,则pom.xml配置如下:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.ModuleApp</mainClass> </configuration> </plugin> </plugins> </build>
这样,运行打包后的JAR时,Spring Boot会自动使用指定的mainClass启动应用。 mainClass属性值必须是全限定类名(包含包名的完整类名)。 正确配置后,重新打包,即可通过运行JAR文件启动选择的启动类。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END