feat: 使用 jdk instanceof 新特性

This commit is contained in:
xingyu4j
2025-08-28 15:10:52 +08:00
parent 495ef72a39
commit 224980aa1a
7 changed files with 12 additions and 20 deletions

View File

@@ -13,11 +13,10 @@ public class TenantRabbitMQInitializer implements BeanPostProcessor {
@Override @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RabbitTemplate) { if (bean instanceof RabbitTemplate rabbitTemplate) {
RabbitTemplate rabbitTemplate = (RabbitTemplate) bean;
rabbitTemplate.addBeforePublishPostProcessors(new TenantRabbitMQMessagePostProcessor()); rabbitTemplate.addBeforePublishPostProcessors(new TenantRabbitMQMessagePostProcessor());
} }
return bean; return bean;
} }
} }

View File

@@ -18,11 +18,9 @@ public class TenantRocketMQInitializer implements BeanPostProcessor {
@Override @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DefaultRocketMQListenerContainer) { if (bean instanceof DefaultRocketMQListenerContainer container) {
DefaultRocketMQListenerContainer container = (DefaultRocketMQListenerContainer) bean;
initTenantConsumer(container.getConsumer()); initTenantConsumer(container.getConsumer());
} else if (bean instanceof RocketMQTemplate) { } else if (bean instanceof RocketMQTemplate template) {
RocketMQTemplate template = (RocketMQTemplate) bean;
initTenantProducer(template.getProducer()); initTenantProducer(template.getProducer());
} }
return bean; return bean;
@@ -50,4 +48,4 @@ public class TenantRocketMQInitializer implements BeanPostProcessor {
consumerImpl.registerConsumeMessageHook(new TenantRocketMQConsumeMessageHook()); consumerImpl.registerConsumeMessageHook(new TenantRocketMQConsumeMessageHook());
} }
} }

View File

@@ -23,15 +23,13 @@ public class YudaoAsyncAutoConfiguration {
@Override @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 处理 ThreadPoolTaskExecutor // 处理 ThreadPoolTaskExecutor
if (bean instanceof ThreadPoolTaskExecutor) { if (bean instanceof ThreadPoolTaskExecutor executor) {
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean;
executor.setTaskDecorator(TtlRunnable::get); executor.setTaskDecorator(TtlRunnable::get);
return executor; return executor;
} }
// 处理 SimpleAsyncTaskExecutor // 处理 SimpleAsyncTaskExecutor
// 参考 https://t.zsxq.com/CBoks 增加 // 参考 https://t.zsxq.com/CBoks 增加
if (bean instanceof SimpleAsyncTaskExecutor) { if (bean instanceof SimpleAsyncTaskExecutor executor) {
SimpleAsyncTaskExecutor executor = (SimpleAsyncTaskExecutor) bean;
executor.setTaskDecorator(TtlRunnable::get); executor.setTaskDecorator(TtlRunnable::get);
return executor; return executor;
} }

View File

@@ -19,8 +19,7 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
@Override @Override
public void insertFill(MetaObject metaObject) { public void insertFill(MetaObject metaObject) {
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO) { if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO baseDO) {
BaseDO baseDO = (BaseDO) metaObject.getOriginalObject();
LocalDateTime current = LocalDateTime.now(); LocalDateTime current = LocalDateTime.now();
// 创建时间为空,则以当前时间为插入时间 // 创建时间为空,则以当前时间为插入时间

View File

@@ -184,8 +184,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(HttpMessageNotReadableException.class) @ExceptionHandler(HttpMessageNotReadableException.class)
public CommonResult<?> methodArgumentTypeInvalidFormatExceptionHandler(HttpMessageNotReadableException ex) { public CommonResult<?> methodArgumentTypeInvalidFormatExceptionHandler(HttpMessageNotReadableException ex) {
log.warn("[methodArgumentTypeInvalidFormatExceptionHandler]", ex); log.warn("[methodArgumentTypeInvalidFormatExceptionHandler]", ex);
if (ex.getCause() instanceof InvalidFormatException) { if (ex.getCause() instanceof InvalidFormatException invalidFormatException) {
InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause();
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue())); return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue()));
} }
if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) { if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) {

View File

@@ -146,10 +146,9 @@ public class WebFrameworkUtils {
public static HttpServletRequest getRequest() { public static HttpServletRequest getRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (!(requestAttributes instanceof ServletRequestAttributes)) { if (!(requestAttributes instanceof ServletRequestAttributes servletRequestAttributes)) {
return null; return null;
} }
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
return servletRequestAttributes.getRequest(); return servletRequestAttributes.getRequest();
} }

View File

@@ -77,8 +77,8 @@ public class AuthRequestFactory {
extendList = extend.getConfig() extendList = extend.getConfig()
.keySet() .keySet()
.stream() .stream()
.filter(x -> names.contains(x.toUpperCase()))
.map(String::toUpperCase) .map(String::toUpperCase)
.filter(names::contains)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@@ -318,4 +318,4 @@ public class AuthRequestFactory {
.proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort()))) .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort())))
.build()); .build());
} }
} }