Merge pull request #7160 from papug145/BAEL-2921

BAEL-2921 Understanding getBean
This commit is contained in:
Eric Martin 2019-06-30 20:18:17 -05:00 committed by GitHub
commit 1318804764
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 286 additions and 0 deletions

View File

@ -660,6 +660,7 @@
<module>spring-cloud-data-flow</module> <module>spring-cloud-data-flow</module>
<module>spring-core</module> <module>spring-core</module>
<module>spring-core-2</module>
<module>spring-cucumber</module> <module>spring-cucumber</module>
<module>spring-data-rest</module> <module>spring-data-rest</module>
@ -1324,6 +1325,7 @@
<module>spring-cloud-data-flow</module> <module>spring-cloud-data-flow</module>
<module>spring-core</module> <module>spring-core</module>
<module>spring-core-2</module>
<module>spring-cucumber</module> <module>spring-cucumber</module>
<module>spring-data-rest</module> <module>spring-data-rest</module>

60
spring-core-2/pom.xml Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-5</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-core-2</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<properties>
<spring.version>5.1.4.RELEASE</spring.version>
<junit.jupiter.version>5.0.2</junit.jupiter.version>
</properties>
</project>

View File

@ -0,0 +1,22 @@
package com.baeldung.getbean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
class AnnotationConfig {
@Bean(name = {"tiger", "kitty"})
@Scope(value = "prototype")
Tiger getTiger(String name) {
return new Tiger(name);
}
@Bean(name = "lion")
Lion getLion() {
return new Lion("Hardcoded lion name");
}
interface Animal {}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.getbean;
class Lion implements AnnotationConfig.Animal {
private String name;
Lion(String name) {
this.name = name;
}
String getName() {
return name;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.getbean;
class Tiger implements AnnotationConfig.Animal {
private String name;
Tiger(String name) {
this.name = name;
}
String getName() {
return name;
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameAndTypeUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenSpecifiedMatchingNameAndType_thenShouldReturnRelatedBean() {
Lion lion = context.getBean("lion", Lion.class);
assertEquals("Hardcoded lion name", lion.getName());
}
@Test
void whenSpecifiedNotMatchingNameAndType_thenShouldThrowException() {
assertThrows(BeanNotOfRequiredTypeException.class, () -> context.getBean("lion", Tiger.class));
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingBeanName_shouldReturnThatBean() {
Object lion = context.getBean("lion");
assertEquals(lion.getClass(), Lion.class);
}
@Test
void whenGivenNonExistingBeanName_shouldThrowException() {
assertThrows(NoSuchBeanDefinitionException.class, () -> context.getBean("non-existing"));
}
@Test
void whenCastingToWrongType_thenShouldThrowException() {
assertThrows(ClassCastException.class, () -> {
Tiger tiger = (Tiger) context.getBean("lion");
});
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameWithConstructorParametersUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenCorrectName_thenShouldReturnBeanWithSpecifiedName() {
Tiger tiger = (Tiger) context.getBean("tiger", "Siberian");
assertEquals("Siberian", tiger.getName());
}
@Test
void whenGivenCorrectNameOrAlias_shouldReturnBeanWithSpecifiedName() {
Tiger tiger = (Tiger) context.getBean("tiger", "Siberian");
Tiger secondTiger = (Tiger) context.getBean("tiger", "Striped");
assertEquals("Siberian", tiger.getName());
assertEquals("Striped", secondTiger.getName());
}
@Test
void whenNoArgumentSpecifiedForPrototypeBean_thenShouldThrowException() {
assertThrows(UnsatisfiedDependencyException.class, () -> {
Tiger tiger = (Tiger) context.getBean("tiger");
});
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByTypeUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingUniqueType_thenShouldReturnRelatedBean() {
Lion lion = context.getBean(Lion.class);
assertNotNull(lion);
}
@Test
void whenGivenAmbiguousType_thenShouldThrowException() {
assertThrows(NoUniqueBeanDefinitionException.class, () -> context.getBean(AnnotationConfig.Animal.class));
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByTypeWithConstructorParametersUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingTypeAndValidParameters_thenShouldReturnRelatedBean() {
Tiger tiger = context.getBean(Tiger.class, "Shere Khan");
assertEquals("Shere Khan", tiger.getName());
}
}