如何控制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修改过]


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

查看所有标签

猜你喜欢:

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

About Face 2.0

About Face 2.0

Alan Cooper、Robert M. Reimann / Wiley / March 17, 2003 / USD 35.00

First published seven years ago-just before the World Wide Web exploded into dominance in the software world-About Face rapidly became a bestseller. While the ideas and principles in the original book......一起来看看 《About Face 2.0》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具