fix: avoid early handler post processor dependencies
This commit is contained in:
@@ -43,8 +43,8 @@ public class IngestCoreAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AnnotationHandlerBeanPostProcessor annotationHandlerBeanPostProcessor(HandlerRegistry registry) {
|
||||
return new AnnotationHandlerBeanPostProcessor(registry);
|
||||
public static AnnotationHandlerBeanPostProcessor annotationHandlerBeanPostProcessor() {
|
||||
return new AnnotationHandlerBeanPostProcessor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -5,9 +5,11 @@ import com.lingniu.ingest.api.annotation.IdempotentKey;
|
||||
import com.lingniu.ingest.api.annotation.MessageMapping;
|
||||
import com.lingniu.ingest.api.annotation.ProtocolHandler;
|
||||
import com.lingniu.ingest.api.annotation.RateLimited;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
@@ -19,16 +21,25 @@ import java.lang.reflect.Method;
|
||||
*
|
||||
* <p>替代旧代码里遍布的 {@code if (msgId == 0x0100) ... else if (msgId == 0x0102) ...} 风格。
|
||||
*/
|
||||
public class AnnotationHandlerBeanPostProcessor implements BeanPostProcessor {
|
||||
public class AnnotationHandlerBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerBeanPostProcessor.class);
|
||||
|
||||
private final HandlerRegistry registry;
|
||||
private HandlerRegistry registry;
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
public AnnotationHandlerBeanPostProcessor() {
|
||||
}
|
||||
|
||||
public AnnotationHandlerBeanPostProcessor(HandlerRegistry registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
Class<?> type = bean.getClass();
|
||||
@@ -53,7 +64,7 @@ public class AnnotationHandlerBeanPostProcessor implements BeanPostProcessor {
|
||||
HandlerDefinition def = new HandlerDefinition(
|
||||
classAnno.protocol(), cmd, info, mapping.desc(),
|
||||
bean, method, paramType, rl, ik, ab);
|
||||
registry.register(def);
|
||||
registry().register(def);
|
||||
log.info("registered handler {} protocol={} command=0x{} info=0x{}",
|
||||
type.getSimpleName() + "#" + method.getName(),
|
||||
classAnno.protocol(), Integer.toHexString(cmd), Integer.toHexString(info));
|
||||
@@ -62,4 +73,14 @@ public class AnnotationHandlerBeanPostProcessor implements BeanPostProcessor {
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private HandlerRegistry registry() {
|
||||
if (registry == null) {
|
||||
if (beanFactory == null) {
|
||||
throw new IllegalStateException("BeanFactory must be set before registering protocol handlers");
|
||||
}
|
||||
registry = beanFactory.getBean(HandlerRegistry.class);
|
||||
}
|
||||
return registry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.lingniu.ingest.core.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class IngestCoreAutoConfigurationTest {
|
||||
|
||||
@Test
|
||||
void beanPostProcessorFactoryMethodIsStaticToAvoidEarlyConfigurationInstantiation() throws Exception {
|
||||
Method method = IngestCoreAutoConfiguration.class.getDeclaredMethod(
|
||||
"annotationHandlerBeanPostProcessor");
|
||||
|
||||
assertThat(Modifier.isStatic(method.getModifiers())).isTrue();
|
||||
assertThat(method.getParameterCount()).isZero();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user