fix(syslog-consumer-rule)解决ObjectMapper多实例冲突

This commit is contained in:
王绍宏
2026-09-14 23:38:47 +08:00
parent 1c07acc01d
commit 668a0aca88
2 changed files with 48 additions and 1 deletions
@@ -3,6 +3,7 @@ package com.common.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@@ -35,7 +36,8 @@ public class RawLogCaptureClusterStore {
@Value("${syslog.raw-capture.cluster-activation-delay-ms:1000}")
private long activationDelayMs = 1000L;
public RawLogCaptureClusterStore(StringRedisTemplate redis, ObjectMapper mapper,
public RawLogCaptureClusterStore(StringRedisTemplate redis,
@Qualifier("httpObjectMapper") ObjectMapper mapper,
@Value("${syslog.raw-capture.node-id:${HOSTNAME:unknown}}")
String configuredNodeId) {
this.redis = redis;
@@ -0,0 +1,45 @@
package com.common.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
class RawLogCaptureClusterStoreWiringTest {
@Test
void usesHttpObjectMapperWhenApplicationHasMultipleObjectMappers() {
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(TestConfiguration.class,
RawLogCaptureClusterStore.class)) {
RawLogCaptureClusterStore store = context.getBean(RawLogCaptureClusterStore.class);
assertSame(context.getBean("httpObjectMapper"),
ReflectionTestUtils.getField(store, "mapper"));
}
}
@Configuration
static class TestConfiguration {
@Bean
ObjectMapper objectMapper() {
return new ObjectMapper();
}
@Bean
ObjectMapper httpObjectMapper() {
return new ObjectMapper();
}
@Bean
StringRedisTemplate stringRedisTemplate() {
return mock(StringRedisTemplate.class);
}
}
}