SpringBoot在Tomcat部署war包

2023-07-19 15:23:52 浏览数 (3)

启动类配置

继承SpringBootServletInitializer

代码语言:javascript复制
@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);
    }
}

打包方式配置

代码语言:javascript复制
		<packaging>war</packaging>

移除内置Tomcat

代码语言:javascript复制
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

WebSocket错误

代码语言:javascript复制
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

Bean按需加载

代码语言:javascript复制
@Configuration
public class WebSocketConfig {
    @Bean
    @ConditionalOnProperty(name = "system.package", havingValue = "jar", matchIfMissing = true)
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

Tomcat设置

Host节点增加Context可以直接通过ip 端口方式访问,需要将appBase清除,防止启动两次应用

代码语言:javascript复制
<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 人点赞