|
29 | 29 | import org.springframework.aot.hint.RuntimeHints;
|
30 | 30 | import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
31 | 31 | import org.springframework.beans.BeansException;
|
| 32 | +import org.springframework.beans.factory.BeanCreationException; |
| 33 | +import org.springframework.beans.factory.DisposableBean; |
| 34 | +import org.springframework.beans.factory.InitializingBean; |
32 | 35 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
| 36 | +import org.springframework.beans.factory.SmartInitializingSingleton; |
33 | 37 | import org.springframework.beans.factory.config.AbstractFactoryBean;
|
34 | 38 | import org.springframework.beans.factory.config.BeanDefinition;
|
35 | 39 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
|
59 | 63 | import static org.assertj.core.api.Assertions.assertThat;
|
60 | 64 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
61 | 65 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
| 66 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
62 | 67 | import static org.assertj.core.api.InstanceOfAssertFactories.type;
|
63 | 68 | import static org.mockito.ArgumentMatchers.any;
|
64 | 69 | import static org.mockito.ArgumentMatchers.eq;
|
@@ -302,6 +307,39 @@ private void assertGetResourceSemantics(ResourceLoader resourceLoader, Class<? e
|
302 | 307 | .isEqualTo("pong:foo");
|
303 | 308 | }
|
304 | 309 |
|
| 310 | + @Test |
| 311 | + void refreshWithRuntimeFailureOnBeanCreationDisposeExistingBeans() { |
| 312 | + BeanE one = new BeanE(); |
| 313 | + context.registerBean("one", BeanE.class, () -> one); |
| 314 | + context.registerBean("two", BeanE.class, () -> new BeanE() { |
| 315 | + @Override |
| 316 | + public void afterPropertiesSet() { |
| 317 | + throw new IllegalStateException("Expected"); |
| 318 | + } |
| 319 | + }); |
| 320 | + assertThatThrownBy(context::refresh).isInstanceOf(BeanCreationException.class) |
| 321 | + .hasMessageContaining("two"); |
| 322 | + assertThat(one.initialized).isTrue(); |
| 323 | + assertThat(one.destroyed).isTrue(); |
| 324 | + } |
| 325 | + |
| 326 | + @Test |
| 327 | + void refreshWithRuntimeFailureOnAfterSingletonInstantiatedDisposeExistingBeans() { |
| 328 | + BeanE one = new BeanE(); |
| 329 | + BeanE two = new BeanE(); |
| 330 | + context.registerBean("one", BeanE.class, () -> one); |
| 331 | + context.registerBean("two", BeanE.class, () -> two); |
| 332 | + context.registerBean("int", SmartInitializingSingleton.class, () -> () -> { |
| 333 | + throw new IllegalStateException("expected"); |
| 334 | + }); |
| 335 | + assertThatThrownBy(context::refresh).isInstanceOf(IllegalStateException.class) |
| 336 | + .hasMessageContaining("expected"); |
| 337 | + assertThat(one.initialized).isTrue(); |
| 338 | + assertThat(two.initialized).isTrue(); |
| 339 | + assertThat(one.destroyed).isTrue(); |
| 340 | + assertThat(two.destroyed).isTrue(); |
| 341 | + } |
| 342 | + |
305 | 343 | @Test
|
306 | 344 | void refreshForAotSetsContextActive() {
|
307 | 345 | GenericApplicationContext context = new GenericApplicationContext();
|
@@ -646,6 +684,29 @@ public void setCounter(Integer counter) {
|
646 | 684 | }
|
647 | 685 | }
|
648 | 686 |
|
| 687 | + static class BeanE implements InitializingBean, DisposableBean { |
| 688 | + |
| 689 | + private boolean initialized; |
| 690 | + |
| 691 | + private boolean destroyed; |
| 692 | + |
| 693 | + @Override |
| 694 | + public void afterPropertiesSet() throws Exception { |
| 695 | + if (initialized) { |
| 696 | + throw new IllegalStateException("AfterPropertiesSet called twice"); |
| 697 | + } |
| 698 | + this.initialized = true; |
| 699 | + } |
| 700 | + |
| 701 | + @Override |
| 702 | + public void destroy() throws Exception { |
| 703 | + if (destroyed) { |
| 704 | + throw new IllegalStateException("Destroy called twice"); |
| 705 | + } |
| 706 | + this.destroyed = true; |
| 707 | + } |
| 708 | + } |
| 709 | + |
649 | 710 |
|
650 | 711 | static class TestAotFactoryBean<T> extends AbstractFactoryBean<T> {
|
651 | 712 |
|
|
0 commit comments