fix 修复 自动装配无法获取spring上下问题问题

This commit is contained in:
疯狂的狮子Li 2024-03-27 15:08:06 +08:00
parent ee9183d40f
commit ee52907a46
7 changed files with 35 additions and 14 deletions

View File

@ -2,6 +2,9 @@ package com.aizuda.easy.retry.common.core.context;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
@ -15,23 +18,42 @@ import org.springframework.stereotype.Component;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class SpringContext implements ApplicationContextAware {
public class SpringContext implements BeanFactoryPostProcessor, ApplicationContextAware {
public static ApplicationContext CONTEXT;
private static ConfigurableListableBeanFactory FACTORY;
private static ApplicationContext CONTEXT;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
SpringContext.FACTORY = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContext.CONTEXT = applicationContext;
}
public static ListableBeanFactory getBeanFactory() {
final ListableBeanFactory factory = null == FACTORY ? CONTEXT : FACTORY;
if (null == factory) {
throw new RuntimeException("No ConfigurableListableBeanFactory or ApplicationContext injected, maybe not in the Spring environment?");
}
return factory;
}
public static ApplicationContext getContext() {
return CONTEXT;
}
public static <T> T getBeanByType(Class<T> clazz) {
return CONTEXT.getBean(clazz);
return getBeanFactory().getBean(clazz);
}
public static synchronized <T> T getBean(String name) {
try {
return (T) CONTEXT.getBean(name);
return (T) getBeanFactory().getBean(name);
} catch (BeansException | NullPointerException exception) {
log.error(" BeanName:{} not existException => {}", name, exception.getMessage());
return null;
@ -40,7 +62,7 @@ public class SpringContext implements ApplicationContextAware {
public static synchronized <T> T getBean(Class<T> requiredType) {
try {
return CONTEXT.getBean(requiredType);
return getBeanFactory().getBean(requiredType);
} catch (BeansException | NullPointerException exception) {
log.error(" BeanName:{} not existException => {}", requiredType.getName(), exception.getMessage());
return null;
@ -49,7 +71,7 @@ public class SpringContext implements ApplicationContextAware {
public static synchronized <T> T getBean(String name, Class<T> requiredType) {
try {
return CONTEXT.getBean(name, requiredType);
return getBeanFactory().getBean(name, requiredType);
} catch (BeansException | NullPointerException exception) {
log.error(" BeanName:{} not existException => {}", name, exception.getMessage());
return null;

View File

@ -69,7 +69,7 @@ public class LogUtils {
private static Boolean getLogStatus() {
try {
Environment environment = SpringContext.CONTEXT.getBean(Environment.class);
Environment environment = SpringContext.getBean(Environment.class);
return environment.getProperty("easy-retry.log.status", Boolean.class, Boolean.TRUE);
} catch (Exception ignored) {
}

View File

@ -1 +0,0 @@
com.aizuda.easy.retry.sqlserver.datasource.config.EasyRetrySqlServerAutoConfiguration

View File

@ -133,7 +133,7 @@ public class RpcClientInvokeHandler implements InvocationHandler {
Assert.notNull(parasResult.body, () -> new EasyRetryServerException("body cannot be null"));
}
RestTemplate restTemplate = SpringContext.CONTEXT.getBean(RestTemplate.class);
RestTemplate restTemplate = SpringContext.getBean(RestTemplate.class);
Retryer<Result> retryer = buildResultRetryer();
@ -169,7 +169,7 @@ public class RpcClientInvokeHandler implements InvocationHandler {
// 进行路由剔除处理
CacheRegisterTable.remove(groupName, namespaceId, hostId);
// 重新选一个可用的客户端节点
ClientNodeAllocateHandler clientNodeAllocateHandler = SpringContext.CONTEXT.getBean(
ClientNodeAllocateHandler clientNodeAllocateHandler = SpringContext.getBean(
ClientNodeAllocateHandler.class);
RegisterNodeInfo serverNode = clientNodeAllocateHandler.getServerNode(allocKey, groupName, namespaceId,
routeKey);

View File

@ -91,7 +91,7 @@ public class JobExecutorActor extends AbstractActor {
} catch (Exception e) {
EasyRetryLog.LOCAL.error("job executor exception. [{}]", taskExecute, e);
handlerTaskBatch(taskExecute, JobTaskBatchStatusEnum.FAIL.getStatus(), JobOperationReasonEnum.TASK_EXECUTION_ERROR.getReason());
SpringContext.CONTEXT.publishEvent(new JobTaskFailAlarmEvent(taskExecute.getTaskBatchId()));
SpringContext.getContext().publishEvent(new JobTaskFailAlarmEvent(taskExecute.getTaskBatchId()));
} finally {
getContext().stop(getSelf());
}

View File

@ -65,7 +65,7 @@ public class JobTaskBatchHandler {
if (failCount > 0) {
jobTaskBatch.setTaskBatchStatus(JobTaskBatchStatusEnum.FAIL.getStatus());
SpringContext.CONTEXT.publishEvent(new JobTaskFailAlarmEvent(completeJobBatchDTO.getTaskBatchId()));
SpringContext.getContext().publishEvent(new JobTaskFailAlarmEvent(completeJobBatchDTO.getTaskBatchId()));
} else if (stopCount > 0) {
jobTaskBatch.setTaskBatchStatus(JobTaskBatchStatusEnum.STOP.getStatus());
} else {

View File

@ -104,7 +104,7 @@ public class RequestHandlerActor extends AbstractActor {
}
UrlBuilder builder = UrlBuilder.ofHttp(uri);
Collection<HttpRequestHandler> httpRequestHandlers = SpringContext.CONTEXT
Collection<HttpRequestHandler> httpRequestHandlers = SpringContext.getContext()
.getBeansOfType(HttpRequestHandler.class).values();
for (HttpRequestHandler httpRequestHandler : httpRequestHandlers) {
if (httpRequestHandler.supports(builder.getPathStr()) && method.name()