SpringBoot easily labeled as a war package

SpringBoot project normally in the form of jar package, via the command line:

java -jar demo.jar

  

To run, and SpringBoot is embedded Tomcat server, so every time you restart the new Tomcat server is used. For this reason, there have been a problem:
to upload files to the project, if it is stored in the project, then the file will be lost after the restart. For example, we have uploaded a picture, restart the project, the head is gone. If you saved the file on the local disk, then, html tag is no way to get (of course, enterprise projects generally have a special picture server). Therefore, we need to SpringBoot items labeled as war package, put it in Tomcat to run.

Modification method
add in the following dependence pom.xml file:

 <!--因配置外部TOMCAT 而配置-->
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

  At the same time, the jar header pom.xml file into war

	<groupId>com.star</groupId>
	<artifactId>yiyong</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<!--<packaging>jar</packaging>-->

  Finally, the class from start

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

  change into

@SpringBootApplication
public class YiyongApplication extends SpringBootServletInitializer{
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(YiyongApplication.class);
	}


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

  

Note: This class inherits SpringBootServletInitializer and rewrite the configure method, which is the key.

Package deployment

IDEA Maven in the right side bar waiting Build Success can double-click the package.

 

 

Then the war package under the target directory into the webapps directory of tomcat, tomcat startup, automatically unzip deployment.
Finally, enter in your browser

http: // localhost: [port number] / [packaged item name] /

  Successfully posted

Guess you like

Origin www.cnblogs.com/jtlgb/p/11970610.html
Recommended