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
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: ComponentScan
you can customize the package directory.
The fifth
Start under the directory, we can decompress the jar
There is a META-INF/MANIFEST.MF
file in the META-INF directory , the MANIFEST.MF
content
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.
Sixth
War package startup: There are two ways to switch from jar to war packaging
Type 1:
In
pom.xml
modify or add inwar
Create
webapp/WEB-INF
directory (relative tosrc/main
)Create an empty
web.xml
Type 2:
1. pom.xml
Modify or add in<packaging>war</packaging>
2. pom.xml
Add 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