SpringBoot在Tomcat部署war包

啓動類配置

繼承SpringBootServletInitializer

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

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

打包方式配置

		<packaging>war</packaging>

移除內置Tomcat

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

WebSocket錯誤

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

Bean按需加載

@Configuration
public class WebSocketConfig {
    @Bean
    @ConditionalOnProperty(name = "system.package", havingValue = "jar", matchIfMissing = true)
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

Tomcat設置

Host節點增加Context可以直接通過ip+端口方式訪問,需要將appBase清除,防止啓動兩次應用

<Host name="localhost"  appBase="" unpackWARs="true" autoDeploy="true">
		<Context path="" docBase="webapps/test" reloadable="false"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

注意事項

對於框架封裝引用jar包,需要注意工程項目中只能有一個類繼承自SpringBootServletInitializer,否則會導致ApplicationContext初始化兩次

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章