Home / Class/ BeanDefinitionLoaderTests Class — spring-boot Architecture

BeanDefinitionLoaderTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/BeanDefinitionLoaderTests.java lines 38–191

class BeanDefinitionLoaderTests {

	private StaticApplicationContext registry;

	@BeforeEach
	void setup() {
		this.registry = new StaticApplicationContext();
	}

	@AfterEach
	void cleanUp() {
		this.registry.close();
	}

	@Test
	void loadClass() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class);
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myComponent")).isTrue();
	}

	@Test
	void anonymousClassNotLoaded() {
		MyComponent myComponent = new MyComponent() {

		};
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
		assertThat(load(loader)).isZero();
	}

	@Test
	void loadJsr330Class() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class);
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
	}

	@Test
	@WithSampleBeansXmlResource
	void loadXmlResource() {
		ClassPathResource resource = new ClassPathResource("org/springframework/boot/sample-beans.xml");
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myXmlComponent")).isTrue();

	}

	@Test
	@WithResource(name = "org/springframework/boot/sample-beans.groovy", content = """
			import org.springframework.boot.sampleconfig.MyComponent;

			beans {
				myGroovyComponent(MyComponent) {}
			}
			""")
	void loadGroovyResource() {
		ClassPathResource resource = new ClassPathResource("org/springframework/boot/sample-beans.groovy");
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();

	}

	@Test
	@WithResource(name = "org/springframework/boot/sample-namespace.groovy", content = """
			import org.springframework.boot.sampleconfig.MyComponent;

			beans {
				xmlns([ctx:'http://www.springframework.org/schema/context'])
				ctx.'component-scan'('base-package':'nonexistent')
				myGroovyComponent(MyComponent) {}
			}
			""")
	void loadGroovyResourceWithNamespace() {
		ClassPathResource resource = new ClassPathResource("org/springframework/boot/sample-namespace.groovy");
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();

	}

	@Test
	void loadPackage() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage());
		assertThat(load(loader)).isEqualTo(2);
		assertThat(this.registry.containsBean("myComponent")).isTrue();
		assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
	}

	@Test
	void loadClassName() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getName());
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myComponent")).isTrue();
	}

	@Test
	@WithSampleBeansXmlResource
	void loadXmlName() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
				"classpath:org/springframework/boot/sample-beans.xml");
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
	}

	@Test
	@WithResource(name = "org/springframework/boot/sample-beans.groovy", content = """
			import org.springframework.boot.sampleconfig.MyComponent;

			beans {
				myGroovyComponent(MyComponent) {}
			}
			""")
	void loadGroovyName() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
				"classpath:org/springframework/boot/sample-beans.groovy");
		assertThat(load(loader)).isOne();
		assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
	}

	@Test
	void loadPackageName() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage().getName());
		assertThat(load(loader)).isEqualTo(2);
		assertThat(this.registry.containsBean("myComponent")).isTrue();
		assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
	}

	@Test
	void loadPackageNameWithoutDot() {
		// See gh-6126
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
				MyComponentInPackageWithoutDot.class.getPackage().getName());
		int loaded = load(loader);
		assertThat(loaded).isOne();
		assertThat(this.registry.containsBean("myComponentInPackageWithoutDot")).isTrue();
	}

	@Test
	void loadPackageAndClassDoesNotDoubleAdd() {
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(),
				MyComponent.class);
		assertThat(load(loader)).isEqualTo(2);
		assertThat(this.registry.containsBean("myComponent")).isTrue();
		assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
	}

	private int load(BeanDefinitionLoader loader) {
		int beans = this.registry.getBeanDefinitionCount();
		loader.load();
		return this.registry.getBeanDefinitionCount() - beans;
	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free