Introduce callback for singleton availability

Closes gh-21362
This commit is contained in:
Juergen Hoeller
2024-03-07 09:57:29 +01:00
parent 1a8d64f9d8
commit c9e85ec297
3 changed files with 44 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.beans.factory.support;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.testfixture.beans.DerivedTestBean;
@@ -35,12 +37,18 @@ class DefaultSingletonBeanRegistryTests {
@Test
void singletons() {
AtomicBoolean tbFlag = new AtomicBoolean();
beanRegistry.addSingletonCallback("tb", instance -> tbFlag.set(true));
TestBean tb = new TestBean();
beanRegistry.registerSingleton("tb", tb);
assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
assertThat(tbFlag.get()).isTrue();
AtomicBoolean tb2Flag = new AtomicBoolean();
beanRegistry.addSingletonCallback("tb2", instance -> tb2Flag.set(true));
TestBean tb2 = (TestBean) beanRegistry.getSingleton("tb2", TestBean::new);
assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2);
assertThat(tb2Flag.get()).isTrue();
assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
assertThat(beanRegistry.getSingleton("tb2")).isSameAs(tb2);