zqy
1 天以前 28cf0afe5cb951bf34a60a1ee0f36d38c592a8c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.ruoyi.common.config;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
 
import java.util.concurrent.Executor;
 
@Configuration
@EnableAsync // 启用异步支持
public class AsyncConfig implements AsyncConfigurer {
 
    @Override
    public Executor getAsyncExecutor() {
        // 1. 创建一个基础的异步任务执行器
        SimpleAsyncTaskExecutor delegate = new SimpleAsyncTaskExecutor();
        // 2. 使用DelegatingSecurityContextAsyncTaskExecutor进行包装
        // 它会自动将主线程的安全上下文绑定到异步任务的线程上
        return new DelegatingSecurityContextAsyncTaskExecutor(delegate);
    }
}