代码之家  ›  专栏  ›  技术社区  ›  Whimusical

奇怪的行为,@component作为两个不同的实例注入

  •  0
  • Whimusical  · 技术社区  · 7 年前

    我有一个这样的模板来测试一些实体

    @Component
    public class TestEntityXListener extends TestAbstractConsumer<EntityX> {
    
        @Override
        @KafkaListener(id = "entity1Listener", topicPattern = "company.v1.cqrs.entityX", containerFactory = "kafkaListenerContainerFactory")
        public void onMessage(final ConsumerRecord<MessageKey, GenericRecord> consumerRecord) {
            log.info("XListener HashCode:"+System.identityHashCode(this));
            super.onMessage(consumerRecord);
        }
    }
    

    最后,我的实体测试如下:

    public class EntityXIntegrationTestIT extends AbstractIntegrationTestIT {
    
       @Autowired private TestEntityXListener entityXListener;
    
       @Test
       public void doTest() throws InterruptedException {
    
            log.info("XListener HashCode:"+System.identityHashCode(this.entityXListener));
            //Execute service that produces Kafka events (logs show correct sending and consumption)
      }
    }
    

    当我在本地(win10)中进行mvn测试时,此代码呈绿色通过,侦听器的所有注入都具有相同的identityhashcode。但是,当我在远程管道中执行此操作时,测试中注入的侦听器实例似乎与事件到达时正在处理的侦听器实例不同

    0 回复  |  直到 7 年前