spring Boot项目多个启动类:Jar包启动入口的选择
在spring boot项目开发中,有时会遇到包含多个启动类的场景。本文将解决如何从多个启动类中选择一个作为打包后Jar包的启动入口的问题。
核心问题在于maven在构建Spring Boot应用的Jar包时,需要明确指定应用程序的入口类。默认情况下,Maven只能识别一个主类作为启动类。如果存在多个带有@SpringBootApplication注解的类,Maven无法自动判断。
解决方案:使用Spring Boot Maven插件配置mainClass
通过Spring Boot Maven插件(spring-boot-maven-plugin)的mainClass配置项,我们可以指定打包时使用的主类。假设项目中有两个启动类:com.example.App1和com.example.App2。要启动com.example.App1,需在pom.xml文件的
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.App1</mainClass> </configuration> </plugin> </plugins> </build>
执行mvn clean package后生成的Jar包将以com.example.App1作为启动类。要启动com.example.App2,只需将mainClass的值更改为com.example.App2即可。 此方法灵活控制最终Jar包的启动入口,方便管理和部署包含多个启动类的Spring Boot应用。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END