88 lines
2.9 KiB
Java
88 lines
2.9 KiB
Java
![]() |
package com.rax.excel;
|
||
|
|
||
|
import com.rax.excel.aop.DynamicNameAspect;
|
||
|
import com.rax.excel.aop.RequestExcelArgumentResolver;
|
||
|
import com.rax.excel.aop.ResponseExcelReturnValueHandler;
|
||
|
import com.rax.excel.config.ExcelConfigProperties;
|
||
|
import com.rax.excel.processor.NameProcessor;
|
||
|
import com.rax.excel.processor.NameSpelExpressionProcessor;
|
||
|
import jakarta.annotation.PostConstruct;
|
||
|
import lombok.RequiredArgsConstructor;
|
||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||
|
import org.springframework.context.annotation.Bean;
|
||
|
import org.springframework.context.annotation.Import;
|
||
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||
|
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* @author lengleng
|
||
|
* @date 2020/3/29
|
||
|
* <p>
|
||
|
* 配置初始化
|
||
|
*/
|
||
|
@AutoConfiguration
|
||
|
@RequiredArgsConstructor
|
||
|
@Import(ExcelHandlerConfiguration.class)
|
||
|
@EnableConfigurationProperties(ExcelConfigProperties.class)
|
||
|
public class ResponseExcelAutoConfiguration {
|
||
|
|
||
|
private final RequestMappingHandlerAdapter requestMappingHandlerAdapter;
|
||
|
|
||
|
private final ResponseExcelReturnValueHandler responseExcelReturnValueHandler;
|
||
|
|
||
|
/**
|
||
|
* SPEL 解析处理器
|
||
|
* @return NameProcessor excel名称解析器
|
||
|
*/
|
||
|
@Bean
|
||
|
@ConditionalOnMissingBean
|
||
|
public NameProcessor nameProcessor() {
|
||
|
return new NameSpelExpressionProcessor();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Excel名称解析处理切面
|
||
|
* @param nameProcessor SPEL 解析处理器
|
||
|
* @return DynamicNameAspect
|
||
|
*/
|
||
|
@Bean
|
||
|
@ConditionalOnMissingBean
|
||
|
public DynamicNameAspect dynamicNameAspect(NameProcessor nameProcessor) {
|
||
|
return new DynamicNameAspect(nameProcessor);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 追加 Excel返回值处理器 到 springmvc 中
|
||
|
*/
|
||
|
@PostConstruct
|
||
|
public void setReturnValueHandlers() {
|
||
|
List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter
|
||
|
.getReturnValueHandlers();
|
||
|
|
||
|
List<HandlerMethodReturnValueHandler> newHandlers = new ArrayList<>();
|
||
|
newHandlers.add(responseExcelReturnValueHandler);
|
||
|
assert returnValueHandlers != null;
|
||
|
newHandlers.addAll(returnValueHandlers);
|
||
|
requestMappingHandlerAdapter.setReturnValueHandlers(newHandlers);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 追加 Excel 请求处理器 到 springmvc 中
|
||
|
*/
|
||
|
@PostConstruct
|
||
|
public void setRequestExcelArgumentResolver() {
|
||
|
List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
|
||
|
List<HandlerMethodArgumentResolver> resolverList = new ArrayList<>();
|
||
|
resolverList.add(new RequestExcelArgumentResolver());
|
||
|
resolverList.addAll(argumentResolvers);
|
||
|
requestMappingHandlerAdapter.setArgumentResolvers(resolverList);
|
||
|
}
|
||
|
|
||
|
}
|