我使用AOP将@ExcelProperty中的列名修改,用来支持国际化,例如这样 @ExcelProperty("${file.name}") private String name; 本地测试好了之后部署在服务器也没有问题,但是过了一段时间之后就会失效,本来应该导出为 文件名/FileName, 就会失效变成 ${file.name}。本人菜鸟,请教一下有没有和我一样的,或者帮我看看代码有什么问题吗? @Aspect @Component @Order(2) public class ExcelExportAop {
private final MessageSource messageSource;
private final HttpServletRequest request;
public static final String PAGE_SIZE = "pageSize";
public static final String PAGE = "page";
public ExcelExportAop(MessageSource messageSource, HttpServletRequest request) {
this.messageSource = messageSource;
this.request = request;
}
@Around(value = "@annotation(excelExport)")
public Object excelExport(ProceedingJoinPoint joinPoint, ExcelExport excelExport) throws Throwable {
try {
HttpServletResponse response = null;
ExportParam exportParam = null;
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof HttpServletResponse) {
response = (HttpServletResponse) arg;
} else if (arg instanceof ExportParam) {
exportParam = (ExportParam) arg;
}
}
if (exportParam == null || exportParam.getExportType() == null || !ExportType.match(exportParam.getExportType())) {
return joinPoint.proceed();
}
String i18n = request.getHeader("i18n");
String language = i18n.substring(0, i18n.indexOf('-'));
String country = i18n.substring(i18n.indexOf('-') + 1);
Field[] declaredFields = excelExport.value().getDeclaredFields();
List<String[]> originalHeaders = new ArrayList<>();
for (Field field : declaredFields) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
String[] headers;
if (excelProperty != null) {
headers = excelProperty.value();
originalHeaders.add(headers);
String[] collect = Arrays.stream(headers).map(header -> {
String i18nCode = this.resolveExcelExportHeader(header);
return messageSource.getMessage(i18nCode, null, new Locale(language, country));
}).toArray(String[]::new);
InvocationHandler invocationHandler = Proxy.getInvocationHandler(excelProperty);
Field memberValue = invocationHandler.getClass().getDeclaredField("memberValues");
memberValue.setAccessible(true);
Map map = (Map) memberValue.get(invocationHandler);
map.put("value", collect);
} else {
originalHeaders.add(null);
}
}
Assert.notNull(response, "HttpServletResponse must not be null.");
if (ExportType.CURRENT.equals(exportParam.getExportType())) {
doExportCurrentPage(exportParam, joinPoint, excelExport, response);
} else {
doExportAll(exportParam, joinPoint, excelExport, response);
}
for (int i = 0; i < declaredFields.length; i++) {
ExcelProperty excelProperty = declaredFields[i].getAnnotation(ExcelProperty.class);
if (excelProperty != null) {
InvocationHandler invocationHandler = Proxy.getInvocationHandler(excelProperty);
Field memberValue = invocationHandler.getClass().getDeclaredField("memberValues");
memberValue.setAccessible(true);
Map map = (Map) memberValue.get(invocationHandler);
map.put("value", originalHeaders.get(i));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void doExportAll(ExportParam exportParam, ProceedingJoinPoint joinPoint, ExcelExport excelExport, HttpServletResponse response) throws Throwable {
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
List<String> paramNameList = Arrays.asList(methodSignature.getParameterNames());
if (paramNameList.contains(PAGE_SIZE)) {
int position = paramNameList.indexOf(PAGE_SIZE);
args[position] = excelExport.maxDataCount();
} else {
for (Object arg : args) {
for (Field field : arg.getClass().getDeclaredFields()) {
if (PAGE_SIZE.equals(field.getName())) {
field.setAccessible(true);
field.set(arg, excelExport.maxDataCount());
break;
}
}
}
}
if (paramNameList.contains(PAGE)) {
int position = paramNameList.indexOf(PAGE);
args[position] = 1;
} else {
for (Object arg : args) {
for (Field field : arg.getClass().getDeclaredFields()) {
if (PAGE.equals(field.getName())) {
field.setAccessible(true);
field.set(arg, 1);
break;
}
}
}
}
Object result = joinPoint.proceed(args);
returnValueProcess(exportParam, response, result, excelExport);
}
private void doExportCurrentPage(ExportParam exportParam, ProceedingJoinPoint joinPoint, ExcelExport excelExport, HttpServletResponse response) throws Throwable {
Object result = joinPoint.proceed();
returnValueProcess(exportParam, response, result, excelExport);
}
private void returnValueProcess(ExportParam exportParam, HttpServletResponse response, Object result, ExcelExport excelExport) throws Exception {
List<?> list;
if (result instanceof List<?>) {
list = (List<?>) result;
ExportExcelUtil.exportExcel(response, excelExport.value(), exportParam.getExportFileName(), list);
} else if (result instanceof DataResult) {
DataResult<?> dataResult = (DataResult<?>) result;
Object data = dataResult.getData();
if (data instanceof List<?>) {
list = (List<?>) data;
ExportExcelUtil.exportExcel(response, excelExport.value(), exportParam.getExportFileName(), list);
} else if (data instanceof PageDataVo) {
PageDataVo<?> pageDataVo = (PageDataVo<?>) data;
list = pageDataVo.getList();
ExportExcelUtil.exportExcel(response, excelExport.value(), exportParam.getExportFileName(), list);
} else {
throw new Exception("Export data must be instance of List<?> or DataResult<list<?>> or DataResult<PageDataVo>.");
}
} else {
throw new Exception("Export data must be instance of List<?> or DataResult<list<?>> or DataResult<PageDataVo>.");
}
}
public String resolveExcelExportHeader(String header){
if (header.startsWith("${") && header.endsWith("}")) {
int beginIndex = header.indexOf("{");
int endIndex = header.indexOf("}");
header = header.substring(beginIndex + 1, endIndex);
}
return header;
}
}