Spring Boot 2.0
spring boot 2.0正式版本发布有段时间了,关于新特性的东西网上随便搜索一大堆,修改了很多东西。由于我需要将公司项目升级到spring cloud,所以想想干脆升级一下spring boot,下面记录我升级的过程。
pom.xml
首先肯定是要将版本号升级到最新的2.0.1.RELEASE
,其次取消的log4j的包,需要手工在pom中添加,如果用到了redis,则需要添加commons-pool2
包。
image.png
yml配置参数改动
原spring.http.或spring.server.下的一些参数,例如我用到了文件上传参数,已修改为sping.servlet.multipart下
redis改动
redis默认实现类从原来的Jedis改为lettuce
yml中参数也需要修改
image.png
/**
* @Autor: Show
* @Description: Redis缓存配置
* @Date: 2018-01-31 13:32
* @Modified By:
* @Remark:
*/
@EnableCaching
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport {
@Bean(name = "redisTemplate")
@SuppressWarnings("unchecked")
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//使用fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory);
return builder.build();
}
}
弃用WebMvcConfigurerAdapter
spring boot 2.0中将原来的WebMvcConfigurerAdapter
替换为WebMvcConfigurer
/**
* @Autor: Show
* @Description:
* @Date: 2018-04-03 8:48
* @Modified By:
* @Remark:
*/
@Configuration
public class DefaultViewConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
Validate验证注解改动
org.hibernate.validator.constraints弃用了几个注解
image.png
例如@NoEmpty
改为javax.validation.constraints.NotEmpty
EmbeddedServletContainerFactory
之前在做http地址转https时用到了这玩意,发现2.0中取消了,如下是新的写法
/**
* @Autor: Show
* @Description:
* @Date: 2018-04-16 12:04
* @Modified By:
* @Remark:
*/
@Configuration
public class TomcatHttpsConfig {
@Value("${server.port}")
private Integer httpsPort;
/*@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector()); // 添加http
return tomcat;
}
/*该写法已经不支持,会编译不通过
private Connector createSslConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(port);
protocol.setSSLEnabled(true);
protocol.setKeystoreFile(file);
protocol.setKeystorePass(password);
return connector;
}*/
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory()
{
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
private Connector redirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(80);
connector.setRedirectPort(httpsPort);
return connector;
}
}
Spring Data MongoDB取消了findOne方法
新版本取消了findOne
改为findById
,findById
返回Optional<T>
类型,所以写法有所改动。
大致遇到了这些问题,然后就可以正常编译启动了。
网友评论