7 ways to start a Spring Boot project, one-time package and talk to you

Today, I will focus on learning 7 ways to start the Spring Boot project.

The first

It is also the most common type of local startup

@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

This method is to execute the main method to start directly. It is also the method most of us start locally.

The second

Type the project into a jar package, and then use the command

java -jar XXX.jar

674e9cbc5759950808bd9aa31dc59ffd.png

You can also add various parameters the day after this command. For example: specify port, set heap memory related parameters, specify a certain environment in multiple environments, etc.

The third

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {         SpringApplication springApplication=new SpringApplication(DemoApplication.class);        //可以自定义监听器等        springApplication.run(args);    }}

The fourth

import org.springframework.boot.SpringApplication;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;@SpringBootConfiguration@EnableAutoConfiguration@ComponentScanpublic class DemoApplication {    public static void main(String[] args) {       SpringApplication.run(DemoApplication.class, args);     }}

This way is more flexible, for example: ComponentScanyou can customize the package directory.

The fifth

Start under the directory, we can decompress the jar

2111c8d36f3d506e713e2cf6f0456049.png

There is a META-INF/MANIFEST.MFfile in the META-INF directory , the MANIFEST.MFcontent

24f8140846db024c53e4ef4bdeb383de.png

The focus here is that Start-Class is our Application class. There is also a Main-Class. When some old projects cannot be started using jar, they need to be started in the package directory. We can start the project like this:

java org.springframework.boot.loader.JarLauncher

To start our project.

ee77f2c54e940d62da67d9f0dac9a8b2.png

Sixth

War package startup: There are two ways to switch from jar to war packaging

Type 1:

  1. In pom.xmlmodify or add inwar

  2. Create webapp/WEB-INF directory (relative to src/main)

  3. Create an emptyweb.xml

Type 2:

1. pom.xmlModify or add in<packaging>war</packaging>

2. pom.xmlAdd the plugin to the file

<plugin>        <artifactId>maven-war-plugin</artifactId>        <configuration>        <failOnMissingWebXml>false</failOnMissingWebXml>        </configuration></plugin>

Tie into war package, start method

java -jar demo.war

Seventh

The principle is the same as that of the main method. Use maven commands;

mvn spring-boot:run

7bbf575fe32fe0f1a1cc292837909aa1.png


Guess you like

Origin blog.51cto.com/10983206/2563022