内容简介:Spring Boot应用程序的主类是一个类,它包含一个启动Spring ApplicationContext的public static void main()方法:默认情况下,如果未明确指定主类,则Spring将在编译时在类路径中搜索,如果找不到或发现多个都无法启动。Spring的Jar包或War包的META-INF/MANIFEST.MF清单如下:
Spring Boot应用程序的主类是一个类,它包含一个启动Spring ApplicationContext的public static void main()方法:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
. ....
}
}
默认情况下,如果未明确指定主类,则Spring将在编译时在类路径中搜索,如果找不到或发现多个都无法启动。
Spring的Jar包或War包的META-INF/MANIFEST.MF清单如下:
Manifest-Version: 1.0 Start-Class: com.example.MyApplication Main-Class: org.springframework.boot.loader.JarLauncher
我们需要在清单中定义Start-Class属性,由JarLauncher或WarLauncher认识它以启动应用。也就是说,我们只有控制定义Start-Class属性才能控制Spring boot的入口。
如何使用Maven控制这个属性?
可以在pom.xml的属性部分中定义为start-class元素:
<properties>
<!-- The main class to start by executing "java -jar" -->
<start-class>com.example.MyApplication</start-class>
</properties>
请注意,只有在我们的pom.xml 中将spring-boot-starter-parent添加为<parent>时, 才能使用此属性。
可以将主类定义为pom.xml的plugin中的spring-boot-maven-plugin的mainClass元素 :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.MyApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
还可以通过命令行界面指定主类:
java -cp bootApp.jar -Dloader.main=com.example.MyApplication org.springframework.boot.loader.PropertiesLauncher
[该贴被banq于2018-08-25 12:27修改过]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Kubernetes入口控制器Contour,成为云原生计算基金会孵化项目
- 源码分析(二)—入口篇
- Vue源码: 构造函数入口
- 链表中环的入口节点
- docker entrypoint入口文件详解
- 自定义MVC框架-入口文件
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
High Performance Python
Micha Gorelick、Ian Ozsvald / O'Reilly Media / 2014-9-10 / USD 39.99
If you're an experienced Python programmer, High Performance Python will guide you through the various routes of code optimization. You'll learn how to use smarter algorithms and leverage peripheral t......一起来看看 《High Performance Python》 这本书的介绍吧!