update 优化代码结构

feature/model
疯狂的狮子Li 1 year ago
parent da41c65213
commit bbe9dc7dc8

@ -2,16 +2,14 @@ package org.dromara.common.core.config;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
/** /**
* *
@ -22,16 +20,12 @@ import java.util.concurrent.ScheduledExecutorService;
@AutoConfiguration @AutoConfiguration
public class AsyncConfig implements AsyncConfigurer { public class AsyncConfig implements AsyncConfigurer {
@Autowired
@Qualifier("scheduledExecutorService")
private ScheduledExecutorService scheduledExecutorService;
/** /**
* @Async 使线 * @Async 使线
*/ */
@Override @Override
public Executor getAsyncExecutor() { public Executor getAsyncExecutor() {
return scheduledExecutorService; return SpringUtils.getBean("scheduledExecutorService");
} }
/** /**

@ -22,18 +22,19 @@ public class ValidatorConfig {
*/ */
@Bean @Bean
public Validator validator(MessageSource messageSource) { public Validator validator(MessageSource messageSource) {
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean(); try (LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean()) {
// 国际化 // 国际化
factoryBean.setValidationMessageSource(messageSource); factoryBean.setValidationMessageSource(messageSource);
// 设置使用 HibernateValidator 校验器 // 设置使用 HibernateValidator 校验器
factoryBean.setProviderClass(HibernateValidator.class); factoryBean.setProviderClass(HibernateValidator.class);
Properties properties = new Properties(); Properties properties = new Properties();
// 设置 快速异常返回 // 设置 快速异常返回
properties.setProperty("hibernate.validator.fail_fast", "true"); properties.setProperty("hibernate.validator.fail_fast", "true");
factoryBean.setValidationProperties(properties); factoryBean.setValidationProperties(properties);
// 加载配置 // 加载配置
factoryBean.afterPropertiesSet(); factoryBean.afterPropertiesSet();
return factoryBean.getValidator(); return factoryBean.getValidator();
}
} }
} }

@ -3,6 +3,7 @@ package org.dromara.common.core.utils;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
/** /**
@ -23,6 +24,10 @@ public class MessageUtils {
* @return * @return
*/ */
public static String message(String code, Object... args) { public static String message(String code, Object... args) {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale()); try {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale());
} catch (NoSuchMessageException e) {
return code;
}
} }
} }

@ -10,6 +10,7 @@ import jakarta.servlet.http.HttpSession;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
@ -19,6 +20,7 @@ import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -101,14 +103,22 @@ public class ServletUtils extends JakartaServletUtil {
* request * request
*/ */
public static HttpServletRequest getRequest() { public static HttpServletRequest getRequest() {
return getRequestAttributes().getRequest(); try {
return getRequestAttributes().getRequest();
} catch (Exception e) {
return null;
}
} }
/** /**
* response * response
*/ */
public static HttpServletResponse getResponse() { public static HttpServletResponse getResponse() {
return getRequestAttributes().getResponse(); try {
return getRequestAttributes().getResponse();
} catch (Exception e) {
return null;
}
} }
/** /**
@ -119,8 +129,33 @@ public class ServletUtils extends JakartaServletUtil {
} }
public static ServletRequestAttributes getRequestAttributes() { public static ServletRequestAttributes getRequestAttributes() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); try {
return (ServletRequestAttributes) attributes; RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
} catch (Exception e) {
return null;
}
}
public static String getHeader(HttpServletRequest request, String name) {
String value = request.getHeader(name);
if (StringUtils.isEmpty(value)) {
return StringUtils.EMPTY;
}
return urlDecode(value);
}
public static Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> map = new LinkedCaseInsensitiveMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
}
return map;
} }
/** /**

Loading…
Cancel
Save