1. 程式人生 > >springboot專案釋出到tomcat容器

springboot專案釋出到tomcat容器

1.下面我簡單寫個例子,springboot專案釋出到tomact容器中

2.新建一個maven工程,在pom.xml新增如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhlk</groupId>
  <artifactId>spring-boot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-boot Maven Webapp</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
 <dependencies>
 		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
  <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
   </dependencies>
   <build>
     <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
            <warName>springboot</warName>
            </configuration>
  </plugin> 
 </plugins>
   </build>
</project>

其中,如下是生成war

 <packaging>war</packaging>

其中,如下依賴是為了脫離專案自帶的tomcat容器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
如下依賴是為了更改專案應用工程名   

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
            <warName>springboot</warName>
            </configuration>
  </plugin> 

3.新建一個HelloApplication類,如下

package com.ly.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Configuration
@SpringBootApplication
public class HelloApplication extends SpringBootServletInitializer {

	@RequestMapping("hello")
	@ResponseBody
	public String hello(){
		return "hello";
	}
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloApplication.class);
    }
	public static void main(String[] args) {
		SpringApplication app=new SpringApplication(HelloApplication.class);
		app.run(args);
	}
}

4.儲存pom.xml,使專案載入完所需要的包,專案右鍵maven更新一下,之後,專案右鍵Run As -Maven build一下 ,執行之後就可以自動生成war


5.找到springboot.war,放到tomact下面的webapps目錄下,解壓也行,不解壓也行,tomact啟動自動會解壓的

6.啟動tomcat,訪問:http://127.0.0.1:8080/springboot/hello