Spring Boot启动容器选择

使用默认内嵌tomcat启动

配置Pom文件:

<packaging>jar</packaging>

<!--添加依赖 -->

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

<!--选择main函数 -->

<plugins>
     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
             <mainClass>com.xx.zc.m.controller.demoController</mainClass>
         </configuration>
     </plugin>
 </plugins>

使用默认内嵌jetty启动

设置Pom文件

 <packaging>jar</packaging>

<!--屏蔽默认的tomcat容器 -->
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

<!--添加jetty引用 -->

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
 </dependency>

<!--添加main函数 -->

 <build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
             <configuration>
                 <mainClass>com.xx.zc.pc.controller.demoController</mainClass>
             </configuration>
         </plugin>
     </plugins>
 </build>

其中Main函数代码如下:

@Controller
@EnableAutoConfiguration
public class demoController {
    public static void main(String[] args) {
        SpringApplication.run(demoController.class, args);
    }

    @RequestMapping("/")
    public String home() {
        return "index";
    }

    @RequestMapping("/now2")
    public String home2() {
        return "index2";
    }
}

启动

java -jar **.jar

后台执行
java -jar spring-boot01-1.0-SNAPSHOT.jar > log.file 2>&1 &

为了构建一个即是可执行的,又能部署到一个外部容器的war文件,你需要标记内嵌容器依赖为”provided“,例如:

<packaging>war</packaging>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

猜你喜欢

转载自blog.csdn.net/yangsnow_rain_wind/article/details/81071827