Переглянути джерело

update 优化 springboot 3.5 新特性与过期代码

疯狂的狮子Li 11 місяців тому
батько
коміт
f47bd39644

+ 5 - 10
ruoyi-admin/src/main/resources/application.yml

@@ -57,6 +57,11 @@ spring:
     # 开启虚拟线程 仅jdk21可用
     virtual:
       enabled: false
+  task:
+    execution:
+      # 从 springboot 3.5 开始 spring自带线程池
+      # 不再需要 AsyncConfig与ThreadPoolConfig 可直接注入线程池使用
+      thread-name-prefix: async-
   # 资源信息
   messages:
     # 国际化资源文件路径
@@ -211,16 +216,6 @@ xss:
   excludeUrls:
     - /system/notice
 
-# 全局线程池相关配置
-# 如使用JDK21请直接使用虚拟线程 不要开启此配置
-thread-pool:
-  # 是否开启线程池
-  enabled: false
-  # 队列最大长度
-  queueCapacity: 128
-  # 线程池维护线程所允许的空闲时间
-  keepAliveSeconds: 300
-
 --- # 分布式锁 lock4j 全局配置
 lock4j:
   # 获取分布式锁超时时间,默认为 3000 毫秒

+ 0 - 52
ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/config/AsyncConfig.java

@@ -1,52 +0,0 @@
-package org.dromara.common.core.config;
-
-import cn.hutool.core.util.ArrayUtil;
-import org.dromara.common.core.exception.ServiceException;
-import org.dromara.common.core.utils.SpringUtils;
-import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
-import org.springframework.boot.autoconfigure.AutoConfiguration;
-import org.springframework.core.task.VirtualThreadTaskExecutor;
-import org.springframework.scheduling.annotation.AsyncConfigurer;
-
-import java.util.Arrays;
-import java.util.concurrent.Executor;
-
-/**
- * 异步配置
- * <p>
- * 如果未使用虚拟线程则生效
- *
- * @author Lion Li
- */
-@AutoConfiguration
-public class AsyncConfig implements AsyncConfigurer {
-
-    /**
-     * 自定义 @Async 注解使用系统线程池
-     */
-    @Override
-    public Executor getAsyncExecutor() {
-        if(SpringUtils.isVirtual()) {
-            return new VirtualThreadTaskExecutor("async-");
-        }
-        return SpringUtils.getBean("scheduledExecutorService");
-    }
-
-    /**
-     * 异步执行异常处理
-     */
-    @Override
-    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
-        return (throwable, method, objects) -> {
-            throwable.printStackTrace();
-            StringBuilder sb = new StringBuilder();
-            sb.append("Exception message - ").append(throwable.getMessage())
-                .append(", Method name - ").append(method.getName());
-            if (ArrayUtil.isNotEmpty(objects)) {
-                sb.append(", Parameter value - ").append(Arrays.toString(objects));
-            }
-            throw new ServiceException(sb.toString());
-        };
-    }
-
-}

+ 0 - 14
ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/config/ThreadPoolConfig.java

@@ -7,11 +7,9 @@ import org.dromara.common.core.config.properties.ThreadPoolProperties;
 import org.dromara.common.core.utils.SpringUtils;
 import org.dromara.common.core.utils.Threads;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.core.task.VirtualThreadTaskExecutor;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -34,18 +32,6 @@ public class ThreadPoolConfig {
 
     private ScheduledExecutorService scheduledExecutorService;
 
-    @Bean(name = "threadPoolTaskExecutor")
-    @ConditionalOnProperty(prefix = "thread-pool", name = "enabled", havingValue = "true")
-    public ThreadPoolTaskExecutor threadPoolTaskExecutor(ThreadPoolProperties threadPoolProperties) {
-        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
-        executor.setCorePoolSize(core);
-        executor.setMaxPoolSize(core * 2);
-        executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());
-        executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());
-        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
-        return executor;
-    }
-
     /**
      * 执行周期性或定时任务
      */

+ 0 - 1
ruoyi-common/ruoyi-common-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -1,5 +1,4 @@
 org.dromara.common.core.config.ApplicationConfig
-org.dromara.common.core.config.AsyncConfig
 org.dromara.common.core.config.ThreadPoolConfig
 org.dromara.common.core.config.ValidatorConfig
 org.dromara.common.core.utils.SpringUtils

+ 4 - 3
ruoyi-extend/ruoyi-monitor-admin/src/main/java/org/dromara/monitor/admin/config/SecurityConfig.java

@@ -12,6 +12,7 @@ import org.springframework.security.config.annotation.web.configurers.HeadersCon
 import org.springframework.security.web.SecurityFilterChain;
 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
 import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
+import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
 import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
 
 /**
@@ -30,7 +31,7 @@ public class SecurityConfig {
     }
 
     @Bean
-    public SecurityFilterChain filterChain(HttpSecurity httpSecurity, MvcRequestMatcher.Builder mvc) throws Exception {
+    public SecurityFilterChain filterChain(HttpSecurity httpSecurity, PathPatternRequestMatcher.Builder mvc) throws Exception {
         SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
         successHandler.setTargetUrlParameter("redirectTo");
         successHandler.setDefaultTargetUrl(adminContextPath + "/");
@@ -40,8 +41,8 @@ public class SecurityConfig {
                 header.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
             .authorizeHttpRequests((authorize) ->
                 authorize.requestMatchers(
-                        mvc.pattern(adminContextPath + "/assets/**"),
-                        mvc.pattern(adminContextPath + "/login")
+                        mvc.matcher(adminContextPath + "/assets/**"),
+                        mvc.matcher(adminContextPath + "/login")
                     ).permitAll()
                     .anyRequest().authenticated())
             .formLogin((formLogin) ->