关于springboot:SpringBoot在Tomcat部署war包

55次阅读

共计 1349 个字符,预计需要花费 4 分钟才能阅读完成。

启动类配置

继承 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 初始化两次

正文完
 0