Skip to content

Commit 1318804

Browse files
authored
Merge pull request eugenp#7160 from papug145/BAEL-2921
BAEL-2921 Understanding getBean
2 parents d289cfa + 1c5e02d commit 1318804

File tree

10 files changed

+286
-0
lines changed

10 files changed

+286
-0
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@
660660
<module>spring-cloud-data-flow</module>
661661

662662
<module>spring-core</module>
663+
<module>spring-core-2</module>
663664
<module>spring-cucumber</module>
664665

665666
<module>spring-data-rest</module>
@@ -1324,6 +1325,7 @@
13241325
<module>spring-cloud-data-flow</module>
13251326

13261327
<module>spring-core</module>
1328+
<module>spring-core-2</module>
13271329
<module>spring-cucumber</module>
13281330

13291331
<module>spring-data-rest</module>

spring-core-2/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>parent-spring-5</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<relativePath>../parent-spring-5</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>spring-core-2</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework</groupId>
18+
<artifactId>spring-beans</artifactId>
19+
<version>${spring.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-context</artifactId>
24+
<version>${spring.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-core</artifactId>
29+
<version>${spring.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-engine</artifactId>
34+
<version>${junit.jupiter.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-api</artifactId>
40+
<version>${junit.jupiter.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-surefire-plugin</artifactId>
50+
<version>2.22.1</version>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<properties>
56+
<spring.version>5.1.4.RELEASE</spring.version>
57+
<junit.jupiter.version>5.0.2</junit.jupiter.version>
58+
</properties>
59+
60+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.getbean;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Scope;
6+
7+
@Configuration
8+
class AnnotationConfig {
9+
10+
@Bean(name = {"tiger", "kitty"})
11+
@Scope(value = "prototype")
12+
Tiger getTiger(String name) {
13+
return new Tiger(name);
14+
}
15+
16+
@Bean(name = "lion")
17+
Lion getLion() {
18+
return new Lion("Hardcoded lion name");
19+
}
20+
21+
interface Animal {}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.getbean;
2+
3+
class Lion implements AnnotationConfig.Animal {
4+
private String name;
5+
6+
Lion(String name) {
7+
this.name = name;
8+
}
9+
10+
String getName() {
11+
return name;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.getbean;
2+
3+
class Tiger implements AnnotationConfig.Animal {
4+
private String name;
5+
6+
Tiger(String name) {
7+
this.name = name;
8+
}
9+
10+
String getName() {
11+
return name;
12+
}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.getbean;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.TestInstance;
6+
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
14+
class GetBeanByNameAndTypeUnitTest {
15+
private ApplicationContext context;
16+
17+
@BeforeAll
18+
void setup() {
19+
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
20+
}
21+
22+
@Test
23+
void whenSpecifiedMatchingNameAndType_thenShouldReturnRelatedBean() {
24+
Lion lion = context.getBean("lion", Lion.class);
25+
26+
assertEquals("Hardcoded lion name", lion.getName());
27+
}
28+
29+
@Test
30+
void whenSpecifiedNotMatchingNameAndType_thenShouldThrowException() {
31+
assertThrows(BeanNotOfRequiredTypeException.class, () -> context.getBean("lion", Tiger.class));
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.getbean;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.TestInstance;
6+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
14+
class GetBeanByNameUnitTest {
15+
private ApplicationContext context;
16+
17+
@BeforeAll
18+
void setup() {
19+
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
20+
}
21+
22+
@Test
23+
void whenGivenExistingBeanName_shouldReturnThatBean() {
24+
Object lion = context.getBean("lion");
25+
26+
assertEquals(lion.getClass(), Lion.class);
27+
}
28+
29+
@Test
30+
void whenGivenNonExistingBeanName_shouldThrowException() {
31+
assertThrows(NoSuchBeanDefinitionException.class, () -> context.getBean("non-existing"));
32+
}
33+
34+
@Test
35+
void whenCastingToWrongType_thenShouldThrowException() {
36+
assertThrows(ClassCastException.class, () -> {
37+
Tiger tiger = (Tiger) context.getBean("lion");
38+
});
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.getbean;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.TestInstance;
6+
import org.springframework.beans.factory.UnsatisfiedDependencyException;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
14+
class GetBeanByNameWithConstructorParametersUnitTest {
15+
private ApplicationContext context;
16+
17+
@BeforeAll
18+
void setup() {
19+
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
20+
}
21+
22+
@Test
23+
void whenGivenCorrectName_thenShouldReturnBeanWithSpecifiedName() {
24+
Tiger tiger = (Tiger) context.getBean("tiger", "Siberian");
25+
26+
assertEquals("Siberian", tiger.getName());
27+
}
28+
29+
@Test
30+
void whenGivenCorrectNameOrAlias_shouldReturnBeanWithSpecifiedName() {
31+
Tiger tiger = (Tiger) context.getBean("tiger", "Siberian");
32+
Tiger secondTiger = (Tiger) context.getBean("tiger", "Striped");
33+
34+
assertEquals("Siberian", tiger.getName());
35+
assertEquals("Striped", secondTiger.getName());
36+
}
37+
38+
@Test
39+
void whenNoArgumentSpecifiedForPrototypeBean_thenShouldThrowException() {
40+
assertThrows(UnsatisfiedDependencyException.class, () -> {
41+
Tiger tiger = (Tiger) context.getBean("tiger");
42+
});
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.getbean;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.TestInstance;
6+
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
10+
import static org.junit.jupiter.api.Assertions.assertNotNull;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
14+
class GetBeanByTypeUnitTest {
15+
private ApplicationContext context;
16+
17+
@BeforeAll
18+
void setup() {
19+
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
20+
}
21+
22+
@Test
23+
void whenGivenExistingUniqueType_thenShouldReturnRelatedBean() {
24+
Lion lion = context.getBean(Lion.class);
25+
26+
assertNotNull(lion);
27+
}
28+
29+
@Test
30+
void whenGivenAmbiguousType_thenShouldThrowException() {
31+
assertThrows(NoUniqueBeanDefinitionException.class, () -> context.getBean(AnnotationConfig.Animal.class));
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.getbean;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.TestInstance;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
12+
class GetBeanByTypeWithConstructorParametersUnitTest {
13+
private ApplicationContext context;
14+
15+
@BeforeAll
16+
void setup() {
17+
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
18+
}
19+
20+
@Test
21+
void whenGivenExistingTypeAndValidParameters_thenShouldReturnRelatedBean() {
22+
Tiger tiger = context.getBean(Tiger.class, "Shere Khan");
23+
24+
assertEquals("Shere Khan", tiger.getName());
25+
}
26+
}

0 commit comments

Comments
 (0)