关于Spring Boot 2.x升3.x的那些事

序言

手头上有个项目,准备从Spring Boot 2.x升级到3.x,升级后发现编译器报了一堆错误。一般来说大版本升级,肯定会有诸多问题,对于程序开发来说能不升就不升。但是对于系统架构来说,能用最新的肯定是用最新的,实在不行再降回去嘛。可是呢,不知道是发布没多久,还是我搜索技巧的问题,很多问题在网上找不到答案。没办法,还是得自己研究,所以呢这次我们就一起来研究一下Spring Boot 3.x究竟有什么改变。

一、关于Spring Session

一般来说,如果一个Spring Boot 2.x项目一开始只需要单实例部署,用不上redis共享会话的话,会在application.properties里加上这个参数。

spring.session.store-type=none

当需要改为多实例部署,需要redis共享会话的时候,只需要改为这样就行了。

spring.session.store-type=redis

但是在Spring Boot 3.x项目里,这个参数就不复存在了。查了Spring Session的官方文档也没有收获。于是去翻Spring Boot的官方文档,在2.x的参考文档中有这么一条提示“You can disable Spring Session by setting the store-type to none.”。而在3.x的文档中,这个提示被删掉了。好家伙,原来store-type=none是直接禁用整个Spring Session,而不是Api文档中所说的"No session data-store."You can disable Spring Session by setting the store-type to none.
那么解决办法就很简单了,单实例部署,不需要用redis的时候,删掉pom.xml里org.springframework.session的依赖就好。需要redis共享会话的时候就要把依赖加回去了,就是没有原来修改配置文件来得方便而已。

二、关于redis

在application.properties里关于redis的配置也有所变化。如果你是这么配置redis的:

spring.redis.host=127.0.0.1
spring.redis.port=6379

这时编译器就会警告你:“Property ‘spring.redis.host’ is Deprecated: Use ‘spring.data.redis.host’ instead.”、“Property ‘spring.redis.password’ is Deprecated: Use ‘spring.data.redis.password’ instead.”按照警告所说的,把“spring.redis”替换成“spring.data.redis”即可

spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379

三、关于servlet

由于tomcat 10包名的更换,如果你的程序是这么写的:

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
...

那么编译器就会报"The import javax.servlet cannot be resolved"错误。原因是包名从javax.servlet 调整为了jakarta.servlet 。解决办法很简单,把javax.servlet 替换为 jakarta.servlet 即可

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
...

四、关于thymeleaf模板

当你使用片段表达式(fragment expression)而没有使用“~{}”时,会获得运行警告。例如你模板里这么写:

<footer th:replace="footer::copy">footer>

则会得到这样的运行警告:“Deprecated unwrapped fragment expression “footer::copy” found in template index, line 7, col 9. Please use the complete syntax of fragment expressions instead (“~{footer::copy}”). The old, unwrapped syntax for fragment expressions will be removed in future versions of Thymeleaf.”
原因是在thymeleaf 3.1中,未封装的片段表达式不再被推荐。解决方法也很简单,按照警告所说的改为完整版的片段表达式,即加上“~{}”即可

<footer th:replace="~{footer::copy}">footer>

五、关于Spring Security

重点来了,随着Spring Boot升级到3.x,Spring Security也升级到了6.x。话不多说,先来看看代码,在6.x之前,如果你想要实现动态权限,你的代码可能会是这样的:

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
   
	
	@Autowired
	MyUserService myUserService;
    @Autowired
    MyUrlFilter myUrlFilter;
    @Autowired
    MyDecisionManager myDecisionManager;
    
	@Bean
	protected PasswordEncoder passwordEncoder() {
   
		return new BCryptPasswordEncoder();
	}
	
	@Bean
	protected SessionRegistry sessionRegistry() {
   
		return new SessionRegistryImpl();
	}
    
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
   
    	auth.userDetailsService(myUserService);
    }
    
    @Override
    public void configure(WebSecurity web) throws Exception {
   
        web.ignoring().antMatchers("/static/**");
    }
    
    @Override
    public void configure(HttpSecurity http) throws Exception{
   
        http.apply(new UrlAuthorizationConfigurer<>(http.getSharedObject(ApplicationContext.class)))
            .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
   
                @Override
                public <O extends FilterSecurityInterceptor> O postProcess(O o) {
   
                    o.setAccessDecisionManager(myDecisionManager);
                    o.setSecurityMetadataSource(myUrlFilter);
                    return o;
                }
            })
            .and().formLogin().loginProcessingUrl("/login/process").loginPage("/login/page")
            .and().logout()

你可能感兴趣的:(spring,boot,java)