一、SpringBootServletInitializer 类所在包的变化
当 SpringBoot 准备打成 war 包部署时,我们需要继承 SpringBootServletInitializer 类。
在 SpringBoot1.5 版本中,SpringBootServletInitializer 类在 org.springframework.boot.web.support 包,而到了 SpringBoot2.2 版本,SpringBootServletInitializer 类迁移到了 org.springframework.boot.web.servlet.support 包。
二、Context-Path 配置项变化
1.5 版本时,配置项目路径使用:
server.context-path=/xxx
,而 2.2 版本改成了:
server.servlet.context-path=/xxx
三、配置项不能再同驼峰名称需改成横线隔开方式
项目中经常使用 @ConfigurationProperties 注解的类来接收配置,这样 SpringBoot 会自动将配置值注入到类属性中。
在 1.5 版本时,只要配置项对上就可以,如:
application.properties
DataSource.driverClassName=org.postgresql.Driver
DataSource.url=jdbc:postgresql://127.0.0.1:7523/test
DataSource.username=root
DataSource.pwd=123456
// DataSourcePropConfig.java
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "DataSource")
public class DataSourcePropConfig {
private String driverClassName;
private String url;
private String username;
private String pwd;
}
而在 SpringBoot2.2 版本中,这样启动则会报错:
Description:
Configuration property name 'DataSource' is not valid:
Invalid characters: 'D', 'S'
Bean: myWebMvcConfigurerAdapter
Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter
Action:
Modify 'DataSource' so that it conforms to the canonical names requirements.
意思就是需要用横线分割的方式定义配置项。解决办法就是改用 @Value 方式注入配置:
// DataSourcePropConfig.java
@Getter
@Setter
@Component
public class DataSourcePropConfig {
@Value("${DataSource.driverClassName}")
private String driverClassName;
@Value("${DataSource.url}")
private String url;
@Value("${DataSource.username}")
private String username;
@Value("${DataSource.pwd}")
private String pwd;
}
四、WebMvcConfigurerAdapter 被废弃
在 1.5 版本时,项目做一些个性化配置(比如:addInterceptors、addCorsMappings、addResourceHandlers 等)可以自定义一个类并继承 WebMvcConfigurerAdapter。但在 2.2 版本中该类被废弃了,因为 SpringBoot2.2 要求至少使用 JDK8,WebMvcConfigurerAdapter 抽象类实现的接口 WebMvcConfigurer 里的方法都已经加上了 default 实现,所以在 2.2 版本中,自定义的类直接实现 WebMvcConfigurer 接口就可以了。
// 1.5
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
// ...
}
// 2.2
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
// ...
}
五、添加 JNDI 数据源的区别
在 1.5 版本中使用 TomcatEmbeddedServletContainerFactory 添加 JNDI 数据源,2.2 版本中 TomcatEmbeddedServletContainerFactory 类不存在了,改成使用 ServletWebServerFactory。
// JNDI 数据源
private ContextResource initDBNamingResource() {
ContextResource resource = new ContextResource();
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "org.postgresql.Driver");
resource.setProperty("url", "jdbc:postgresql://127.0.0.1:7523/test");
resource.setProperty("username", "root");
resource.setProperty("pwaaword", "123456");
// 其它 property 设置。。。
return resource;
}
// 1.5
@Bean
public TomcatEmbeddedServletContainerFactory servletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
tomcat.enableNaming(); // 打开 JNDI 数据源
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
context.getNamingResources().addResource(initDBNamingResource());
}
};
}
// 2.2
@Bean
public ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
tomcat.enableNaming(); // 打开 JNDI 数据源
return super.getTomcatWebServer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
context.getNamingResources().addResource(initDBNamingResource());
}
};
}
五、tomcat 参数调整
1.x
server.tomcat.max-http-post-size=1048576
server.tomcat.max-threads=1000
2.x
server.tomcat.max-http-form-post-size=1048576
server.tomcat.threads.max=1000