如何控制Spring Boot应用入口?

栏目: Java · 发布时间: 7年前

内容简介: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修改过]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ASP.NET 2.0入门经典

ASP.NET 2.0入门经典

哈特 / 清华大学出版社 / 2006-8 / 78.00元

该书是Wrox红皮书中的畅销品种, 从初版、1.0版、1.1版到目前的2.0版,已经3次升级,不仅内容更加完善、实用,还展现了.NET 2.0的最新技术和ASP.NET 2.0最新编程知识,是各种初学者学习ASP.NET的优秀教程,也是Web开发人员了解ASP.NET 2.0新技术的优秀参考书。该书与《ASP.NET 2.0高级编程(第4版)》及其早期版本,曾影响到无数中国Web程序员。一起来看看 《ASP.NET 2.0入门经典》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具