springboot 一般使用jar 的方式运行,我们需要将程序放到tomcat环境下运行。

步骤如下:

1.修改pom文件。

排除内置的tomcat 

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
<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>

修改打包方式

 <packaging>war</packaging>

jar的方式改成 war打包。

2.修改启动代码

@SpringBootApplication
@ImportResource("classpath:transaction.xml")
@MapperScan({"com.neo.dao"}) 
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication app=new SpringApplication(DemoApplication.class);
        app.addListeners(new ApplicationStartedEventListener());
        app.addListeners(new ApplicationStartingEventListener());
        app.addListeners(new ApplicationStartedEventListener2());
        
        app.run(args);
    }
    
     @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
    
}

增加代码

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

3.进行打包

将打包后的代码放到tomcat下执行就可以了。


posted on 2018-10-17 09:12  自由港  阅读(7330)  评论(0编辑  收藏  举报