我有以下代码打破引用透明度:
val producerSettings = ProducerSettings(system, new StringSerializer, new StringSerializer)
.withBootstrapServers("localhost:9092")
val kafkaProducer1 = producerSettings.createKafkaProducer()
val kafkaProducer2 = producerSettings.createKafkaProducer()
println(kafkaProducer1)
println(kafkaProducer2)
输出结果为:
org.apache.kafka.clients.producer.KafkaProducer@49c7b90e
org.apache.kafka.clients.producer.KafkaProducer@10d307f1
正如您所看到的,每次调用方法createKafkaProducer()
时,它都会生成 Kafka Producer 的新实例。
如何避免,它打破了引用透明度?