Home / Class/ LazyInitializationBeanFactoryPostProcessorTests Class — spring-boot Architecture

LazyInitializationBeanFactoryPostProcessorTests Class — spring-boot Architecture

Architecture documentation for the LazyInitializationBeanFactoryPostProcessorTests class in LazyInitializationBeanFactoryPostProcessorTests.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/LazyInitializationBeanFactoryPostProcessorTests.java lines 36–105

class LazyInitializationBeanFactoryPostProcessorTests {

	@Test
	void whenLazyInitializationIsEnabledThenNormalBeansAreNotInitializedUntilRequired() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
			context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
			context.register(BeanState.class, ExampleBean.class);
			context.refresh();
			BeanState beanState = context.getBean(BeanState.class);
			assertThat(beanState.initializedBeans).isEmpty();
			context.getBean(ExampleBean.class);
			assertThat(beanState.initializedBeans).containsExactly(ExampleBean.class);
		}
	}

	@Test
	void whenLazyInitializationIsEnabledThenSmartInitializingSingletonsAreInitializedDuringRefresh() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
			context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
			context.register(BeanState.class, ExampleSmartInitializingSingleton.class);
			context.refresh();
			BeanState beanState = context.getBean(BeanState.class);
			assertThat(beanState.initializedBeans).containsExactly(ExampleSmartInitializingSingleton.class);
			assertThat(context.getBean(ExampleSmartInitializingSingleton.class).callbackInvoked).isTrue();
		}
	}

	@Test
	void whenLazyInitializationIsEnabledThenInfrastructureRoleBeansAreInitializedDuringRefresh() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
			context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
			context.register(BeanState.class);
			context.registerBean(ExampleBean.class,
					(definition) -> definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE));
			context.refresh();
			BeanState beanState = context.getBean(BeanState.class);
			assertThat(beanState.initializedBeans).containsExactly(ExampleBean.class);
		}
	}

	static class ExampleBean {

		ExampleBean(BeanState beanState) {
			beanState.initializedBeans.add(getClass());
		}

	}

	static class ExampleSmartInitializingSingleton implements SmartInitializingSingleton {

		private boolean callbackInvoked;

		ExampleSmartInitializingSingleton(BeanState beanState) {
			beanState.initializedBeans.add(getClass());
		}

		@Override
		public void afterSingletonsInstantiated() {
			this.callbackInvoked = true;
		}

	}

	static class BeanState {

		private final List<Class<?>> initializedBeans = new ArrayList<>();

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free