springboot 使用第三方tomcat 运行

将这个 spring Boot 项目的打包方式设置为 war。

war

去除內嵌tomcat

SpringBoot 默认有内嵌的 tomcat 模块,因此,我们要把这一部分排除掉。(注意不放在第三方tomcat不要去掉),可以先自测没问题,再去除,当然不去除在第三方tomcat下也可以运行

 
        org.springframework.boot
        spring-boot-starter-web
        
           
                  
                    org.springframework.boot  
                    spring-boot-starter-tomcat  
                  
             
    


完整maven配置


  4.0.0
  springboot
  springboot-test
  0.0.1-SNAPSHOT
  springboot-test
  war
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.3.RELEASE


    
        org.springframework.boot
        spring-boot-starter-web
        
           
                  
                    org.springframework.boot  
                    spring-boot-starter-tomcat  
                  
             
    

    javax.servlet
    servlet-api
    3.0-alpha-1


    


  
        
        
         
            
				    org.apache.maven.plugins
				    maven-war-plugin
				    
				        springboot
				    
				
        
    


项目结构

springboot-test
├── pom.xml
├── src
│   ├── main
│   │   ├── index.jsp
│   │   ├── java
│   │   │   └── com
│   │   │       └── lhy
│   │   │           ├── controller
│   │   │           │   └── SampleController.java
│   │   │           └── init
│   │   │               └── Application.java
│   │   └── resources
│   │       └── application.properties
│   └── test
│       ├── java
│       └── resources
├── target
│   ├── classes
│   ├── m2e-jee
│   └── test-classes
└── work
    └── Tomcat
        └── localhost
            └── aaa

springboot 使用第三方tomcat 运行_第1张图片

注意,程序不要放在默认的default package 目录下,不然有可能会报错

main程序,Application.java

启动tomcat会自动找

提供一个 SpringBootServletInitializer 子类,并覆盖它的 configure 方法。我们可以把应用的主类改为继承 SpringBootServletInitializer。或者另外写一个类。

package com.lhy.init;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration  
@ComponentScan(basePackages="com.lhy")   //默认扫描是当前包下的路径
@EnableAutoConfiguration  
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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


controller 类SampleController.java

package com.lhy.controller;


import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello world";
    }


}


打包运行

注意:部署到 tomcat 以后,访问这个项目的时候,须要带上这个项目的 war 包名。

另外,我们还可以使用 war 插件来定义打包以后的 war 包名称,以免 maven 为我们默认地起了一个带版本号的 war 包名称。例如:

  
				    org.apache.maven.plugins
				    maven-war-plugin
				    
				        springboot
				    
				

把打包好的springboot.war 放在tomcat中,启动。

输入 localhost:8080/springboot




你可能感兴趣的:(springboot 使用第三方tomcat 运行)