1Bản GoF không dùng Spring: constructor của AppConfig là private, và AppConfig.getInstance() trả về Holder.INSTANCE. JVM chỉ nạp lớp Holder ở lần gọi đầu, nên app.properties được đọc đúng một lần và an toàn với nhiều thread.The GoF version without Spring: AppConfig's constructor is private and AppConfig.getInstance() returns Holder.INSTANCE. The JVM loads Holder only on the first call, so app.properties is read exactly once, thread-safely.
2Trong ứng dụng Spring, ApplicationContext tạo bean ExchangeRateService một lần khi khởi động (scope singleton mặc định) và truyền FxClient vào constructor. Cache ConcurrentHashMap lúc này còn trống.In the Spring app, the ApplicationContext creates the ExchangeRateService bean once at startup (default singleton scope) and passes FxClient into its constructor. The ConcurrentHashMap cache is empty.
3Spring inject đúng bean đó vào constructor của CheckoutService. Không có lời gọi getInstance() tĩnh nên dependency hiện rõ và test có thể truyền FxClient giả.Spring injects that same bean into CheckoutService's constructor. There is no static getInstance() call, so the dependency is visible and tests can pass a fake FxClient.
4toVnd(10) gọi fx.rate("USD"). USD chưa có trong cache nên computeIfAbsent gọi client.fetchRate("USD") và lưu kết quả; nhiều thread cùng hỏi một key cũng chỉ nạp một lần.toVnd(10) calls fx.rate("USD"). USD is not cached, so computeIfAbsent calls client.fetchRate("USD") and stores the result; concurrent threads asking for the same key still load it once.
5Lần gọi rate("USD") tiếp theo, từ CheckoutService hay bất kỳ bean nào nhận cùng ExchangeRateService, trả ngay giá trị đã lưu. Cache vẫn cần hết hạn; pod khác có instance riêng.The next rate("USD") call, from CheckoutService or any bean holding the same ExchangeRateService, returns the cached value at once. The cache still needs expiry; another pod has its own instance.
6Bản Go: main gọi config.Get() một lần ở rìa chương trình. once.Do chạy hàm khởi tạo đúng một lần, đọc DB_URL và SMS_TOKEN từ biến môi trường vào instance *Config.The Go version: main calls config.Get() once at the program's edge. once.Do runs the initializer exactly once, reading DB_URL and SMS_TOKEN from the environment into the *Config instance.
7main chạy ba goroutine, mỗi cái gọi checkout(cfg, 10) với cfg được truyền vào thay vì gọi config.Get() bên trong. Goroutine đầu tiên chạm rates() làm sync.OnceValue nạp bảng tỉ giá.main starts three goroutines, each calling checkout(cfg, 10) with cfg passed in rather than calling config.Get() inside. The first goroutine to touch rates() makes sync.OnceValue load the rate table.
8Hai goroutine còn lại nhận cùng map đã nạp, nên "loading rates..." chỉ in một lần. Mỗi goroutine in "db:" kèm DBURL rồi in 10 x 25400.The other two goroutines receive the same loaded map, so "loading rates..." prints only once. Each goroutine prints "db:" with the DBURL, then 10 x 25400.