一、改造背景
当业务系统要求部署至国产化中间件TongWeb(非嵌入模式)时,原有SpringBoot的JAR打包方式面临两大挑战:
- 容器冲突:内嵌Tomcat与TongWeb的Servlet容器产生端口/类加载冲突
- 规范不符:TongWeb需要标准的WAR文件结构与WEB-INF/web.xml声明(或Java Config替代方案)
建议SpringBoot 2.x + TongWeb 7.0.x组合(经测试稳定)
二、核心改造方案(附代码片段)
1.打包配置调整
- SpringBoot项目默认打包方式为JAR需显式指定为WAR
- scope -- 指定作用域为编译测试阶段不参与打包
<!-- pom.xml关键修改 -->
<packaging>war</packaging> <!-- 第一步:修改打包类型 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> <!-- 第二步:排除内嵌容器 -->
</dependency>
2.启动类改造
通过继承SpringBootServletInitializer实现WAR模式启动
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);
}
}
3.部署验证
执行mvn clean package后,检查target/目录是否生成.war文件,并通过TongWeb管理控制台完成部署。
二、典型问题解决方案
1.@WebFilter过滤器中Spring Bean注入失效
- 现象:使用@Autowired或@Value获取的Bean为null
注释掉无效的@Value,修改init方法
//@Value("${project.model}")
private String model;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ServletContext context = filterConfig.getServletContext();
ApplicationContext ctx= WebApplicationContextUtils.getWebApplicationContext(context);
testDao = ctx.getBean(TestDao.class);//获取Bean实例
Environment e = ctx.getEnvironment();//获取配置属性
this.model = e.getProperty("project.model");
}
三、迁移收益
合规性:满足国产化中间件部署要求
性能提升:TongWeb的线程池管理比内嵌Tomcat更适配高并发场景
运维便利:可通过TongWeb控制台实现热部署、会话管理等企业级功能