diff --git a/pom.xml b/pom.xml index 6365e407f5..45b6818e66 100644 --- a/pom.xml +++ b/pom.xml @@ -660,6 +660,7 @@ spring-cloud-data-flow spring-core + spring-core-2 spring-cucumber spring-data-rest @@ -1324,6 +1325,7 @@ spring-cloud-data-flow spring-core + spring-core-2 spring-cucumber spring-data-rest diff --git a/spring-core-2/pom.xml b/spring-core-2/pom.xml new file mode 100644 index 0000000000..8e4c539e3b --- /dev/null +++ b/spring-core-2/pom.xml @@ -0,0 +1,60 @@ + + + + com.baeldung + parent-spring-5 + 0.0.1-SNAPSHOT + ../parent-spring-5 + + 4.0.0 + + spring-core-2 + + + + org.springframework + spring-beans + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-core + ${spring.version} + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + + + + 5.1.4.RELEASE + 5.0.2 + + + \ No newline at end of file diff --git a/spring-core-2/src/main/java/com/baeldung/getbean/AnnotationConfig.java b/spring-core-2/src/main/java/com/baeldung/getbean/AnnotationConfig.java new file mode 100644 index 0000000000..12e3ab55e3 --- /dev/null +++ b/spring-core-2/src/main/java/com/baeldung/getbean/AnnotationConfig.java @@ -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 {} +} diff --git a/spring-core-2/src/main/java/com/baeldung/getbean/Lion.java b/spring-core-2/src/main/java/com/baeldung/getbean/Lion.java new file mode 100644 index 0000000000..96c569aeb9 --- /dev/null +++ b/spring-core-2/src/main/java/com/baeldung/getbean/Lion.java @@ -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; + } +} diff --git a/spring-core-2/src/main/java/com/baeldung/getbean/Tiger.java b/spring-core-2/src/main/java/com/baeldung/getbean/Tiger.java new file mode 100644 index 0000000000..85b9626c79 --- /dev/null +++ b/spring-core-2/src/main/java/com/baeldung/getbean/Tiger.java @@ -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; + } +} diff --git a/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java new file mode 100644 index 0000000000..e06804c28e --- /dev/null +++ b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java @@ -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)); + } +} diff --git a/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java new file mode 100644 index 0000000000..4d6d77e39d --- /dev/null +++ b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java @@ -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"); + }); + } +} diff --git a/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java new file mode 100644 index 0000000000..32d37e4ff2 --- /dev/null +++ b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java @@ -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"); + }); + } +} diff --git a/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java new file mode 100644 index 0000000000..a4d4ab732b --- /dev/null +++ b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java @@ -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)); + } +} diff --git a/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java new file mode 100644 index 0000000000..08bcb92145 --- /dev/null +++ b/spring-core-2/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java @@ -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()); + } +}