From b85fde323c8e383adef4b6113e2660c95d2a9f32 Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 15 Mar 2021 13:36:20 +0100 Subject: [PATCH 1/4] BAEL-4785 | Add scenarios to demonstrate the spring naming strategies --- .../springbean/naming/component/Animal.java | 5 ++ .../springbean/naming/component/Cat.java | 14 ++++ .../naming/component/CustomComponent.java | 7 ++ .../springbean/naming/component/Dog.java | 13 ++++ .../configuration/AuditConfiguration.java | 14 ++++ .../naming/configuration/MyConfiguration.java | 19 +++++ .../controller/MessagingController.java | 12 ++++ .../naming/gateway/LoggingGateway.java | 7 ++ .../naming/service/AuditService.java | 4 ++ .../naming/service/LoggingService.java | 12 ++++ .../naming/service/MessagingService.java | 4 ++ .../naming/service/MessagingServiceImpl.java | 14 ++++ .../springbean/naming/service/PetShow.java | 26 +++++++ .../naming/SpringBeanNamingTest.java | 70 +++++++++++++++++++ 14 files changed, 221 insertions(+) create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Animal.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/component/CustomComponent.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Dog.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/AuditConfiguration.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/MyConfiguration.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/controller/MessagingController.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/service/AuditService.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingService.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java create mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/service/PetShow.java create mode 100644 spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Animal.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Animal.java new file mode 100644 index 0000000000..d29b4139c9 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Animal.java @@ -0,0 +1,5 @@ +package com.baeldung.springbean.naming.component; + +public interface Animal { + String name(); +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java new file mode 100644 index 0000000000..a3791ddf60 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java @@ -0,0 +1,14 @@ +package com.baeldung.springbean.naming.component; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +@Component +@Qualifier("cat") +public class Cat implements Animal{ + + @Override + public String name() { + return "Cat"; + } +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/CustomComponent.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/CustomComponent.java new file mode 100644 index 0000000000..71ebfea1a2 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/CustomComponent.java @@ -0,0 +1,7 @@ +package com.baeldung.springbean.naming.component; + +import org.springframework.stereotype.Component; + +@Component("myBean") +public class CustomComponent { +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Dog.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Dog.java new file mode 100644 index 0000000000..a6d6015ab3 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Dog.java @@ -0,0 +1,13 @@ +package com.baeldung.springbean.naming.component; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +@Component +@Qualifier("dog") +public class Dog implements Animal { + @Override + public String name() { + return "Dog"; + } +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/AuditConfiguration.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/AuditConfiguration.java new file mode 100644 index 0000000000..a29dc6ada1 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/AuditConfiguration.java @@ -0,0 +1,14 @@ +package com.baeldung.springbean.naming.configuration; + +import com.baeldung.springbean.naming.service.AuditService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class AuditConfiguration { + + @Bean + public AuditService audit() { + return new AuditService(); + } +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/MyConfiguration.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/MyConfiguration.java new file mode 100644 index 0000000000..f1a18bac95 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/configuration/MyConfiguration.java @@ -0,0 +1,19 @@ +package com.baeldung.springbean.naming.configuration; + +import com.baeldung.springbean.naming.component.CustomComponent; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration("configuration") +public class MyConfiguration { + + @Bean("qualifierComponent") + public CustomComponent component() { + return new CustomComponent(); + } + + @Bean("beanComponent") + public CustomComponent myComponent() { + return new CustomComponent(); + } +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/controller/MessagingController.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/controller/MessagingController.java new file mode 100644 index 0000000000..89d32aba80 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/controller/MessagingController.java @@ -0,0 +1,12 @@ +package com.baeldung.springbean.naming.controller; + +import com.baeldung.springbean.naming.service.MessagingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +@Controller +public class MessagingController { + + @Autowired + private MessagingService service; +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java new file mode 100644 index 0000000000..f2157a5eeb --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java @@ -0,0 +1,7 @@ +package com.baeldung.springbean.naming.gateway; + +import org.springframework.stereotype.Service; + +@Service +public class LoggingGateway { +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/AuditService.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/AuditService.java new file mode 100644 index 0000000000..07811c8104 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/AuditService.java @@ -0,0 +1,4 @@ +package com.baeldung.springbean.naming.service; + +public class AuditService { +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java new file mode 100644 index 0000000000..9276706f7f --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java @@ -0,0 +1,12 @@ +package com.baeldung.springbean.naming.service; + +import com.baeldung.springbean.naming.gateway.LoggingGateway; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class LoggingService { + + @Autowired + private LoggingGateway gateway; +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingService.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingService.java new file mode 100644 index 0000000000..2494712dd3 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingService.java @@ -0,0 +1,4 @@ +package com.baeldung.springbean.naming.service; + +public interface MessagingService { +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java new file mode 100644 index 0000000000..ec2972dfef --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java @@ -0,0 +1,14 @@ +package com.baeldung.springbean.naming.service; + +import com.baeldung.springbean.naming.component.CustomComponent; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class MessagingServiceImpl implements MessagingService{ + + @Autowired + @Qualifier("qualifierComponent") + private CustomComponent customComponent; +} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/PetShow.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/PetShow.java new file mode 100644 index 0000000000..dc74bef4d0 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/PetShow.java @@ -0,0 +1,26 @@ +package com.baeldung.springbean.naming.service; + +import com.baeldung.springbean.naming.component.Animal; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class PetShow { + + @Autowired + @Qualifier("dog") + private Animal dog; + + @Autowired + @Qualifier("cat") + private Animal cat; + + public Animal getDog() { + return dog; + } + + public Animal getCat() { + return cat; + } +} diff --git a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java new file mode 100644 index 0000000000..9e38bf7ae2 --- /dev/null +++ b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java @@ -0,0 +1,70 @@ +package com.baeldung.springbean.naming; + +import com.baeldung.springbean.naming.service.PetShow; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@ExtendWith(SpringExtension.class) +public class SpringBeanNamingTest { + + private AnnotationConfigApplicationContext context; + + @BeforeEach + void setUp() { + context = new AnnotationConfigApplicationContext(); + context.scan("com.baeldung.springbean.naming"); + context.refresh(); + } + + // To name a bean spring gets the class name and converts the first letter to lowercase. + // Default naming strategy of the spring bean which is using class level annotation + + @Test + void givenLoggingServiceBeanIsCreated_whenThereIsNoValueProvided_thenBeanNameShouldBeDefaultName() { + assertNotNull(context.getBean("loggingService")); + } + + // In this case, to name a bean spring gets the class name and converts the first letter to lowercase. + @Test + void givenAuditServiceBeanIsCreatedWithMethodLevelAnnotation_whenThereIsNoValueProvided_thenBeanNameShouldBeTheNameOfMethod() { + assertNotNull(context.getBean("audit")); + } + + // To name a bean spring gets the class name and converts the first letter to lowercase. + // Default naming strategy of the spring bean which is using class level annotation + @Test + void givenLoggingGatewayBeanIsCreatedWithFieldLevelAnnotation_whenThereIsNoValueProvided_thenBeanNameShouldBeDefaultName() { + assertNotNull(context.getBean("loggingGateway")); + } + + // spring will create the bean of type CustomComponent with the name "myBean". + // As we're explicitly giving the name to the bean, spring will use this name to refer to it. + @Test + void givenCustomComponentBeanIsCreate_whenThereIsCustomNameGivenToBean_thenBeanShouldBeIdentifiedByThatName() { + assertNotNull(context.getBean("myBean")); + } + + @Test + void givenCustomComponentBeanIsCreated_whenCustomNameIsGivenOnMethodLevelAnnotation_thenBeanShouldBeIdentifiedByThatName() { + assertNotNull(context.getBean("beanComponent")); + assertNotNull(context.getBean("configuration")); + assertNotNull(context.getBean("qualifierComponent")); + } + + @Test + void givenMultipleImplementationsOfAnimal_whenFieldIsInjectedWithQualifiedName_thenTheSpecificBeanShouldGetInjected() { + PetShow petShow = (PetShow) context.getBean("petShow"); + + assertNotNull(context.getBean("cat")); + assertNotNull(context.getBean("dog")); + + assertThat(petShow.getCat().name()).isEqualTo("Cat"); + assertThat(petShow.getDog().name()).isEqualTo("Dog"); + } +} From 8c2db90f7fbf887b7fbaf338b437d17aaafa0e7e Mon Sep 17 00:00:00 2001 From: vishal Date: Sat, 10 Apr 2021 16:04:51 +0200 Subject: [PATCH 2/4] BAEL-4785 | Refactor | assert the beans with the class instead of bean name --- .../baeldung/springbean/naming/SpringBeanNamingTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java index 9e38bf7ae2..66503f7c26 100644 --- a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java +++ b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java @@ -1,5 +1,7 @@ package com.baeldung.springbean.naming; +import com.baeldung.springbean.naming.component.Cat; +import com.baeldung.springbean.naming.component.Dog; import com.baeldung.springbean.naming.service.PetShow; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -64,7 +66,7 @@ public class SpringBeanNamingTest { assertNotNull(context.getBean("cat")); assertNotNull(context.getBean("dog")); - assertThat(petShow.getCat().name()).isEqualTo("Cat"); - assertThat(petShow.getDog().name()).isEqualTo("Dog"); + assertThat(petShow.getCat().getClass()).isEqualTo(Cat.class); + assertThat(petShow.getDog().getClass()).isEqualTo(Dog.class); } } From e183a0a9e58bdc28562c0a3bfc4993d11d427b48 Mon Sep 17 00:00:00 2001 From: vishal Date: Sat, 10 Apr 2021 17:42:18 +0200 Subject: [PATCH 3/4] BAEL-4785 | Refactor | indentation and pmd violation fix --- .../springbean/naming/component/Cat.java | 2 +- .../naming/service/MessagingServiceImpl.java | 2 +- ...ngTest.java => SpringBeanNamingUnitTest.java} | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) rename spring-core-5/src/test/java/com/baeldung/springbean/naming/{SpringBeanNamingTest.java => SpringBeanNamingUnitTest.java} (77%) diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java index a3791ddf60..8c70078822 100644 --- a/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/component/Cat.java @@ -5,7 +5,7 @@ import org.springframework.stereotype.Component; @Component @Qualifier("cat") -public class Cat implements Animal{ +public class Cat implements Animal { @Override public String name() { diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java index ec2972dfef..2b7cb84742 100644 --- a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/MessagingServiceImpl.java @@ -6,7 +6,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service -public class MessagingServiceImpl implements MessagingService{ +public class MessagingServiceImpl implements MessagingService { @Autowired @Qualifier("qualifierComponent") diff --git a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java similarity index 77% rename from spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java rename to spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java index 66503f7c26..a942897cdd 100644 --- a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingTest.java +++ b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java @@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; @ExtendWith(SpringExtension.class) -public class SpringBeanNamingTest { +public class SpringBeanNamingUnitTest { private AnnotationConfigApplicationContext context; @@ -24,29 +24,29 @@ public class SpringBeanNamingTest { context.refresh(); } - // To name a bean spring gets the class name and converts the first letter to lowercase. - // Default naming strategy of the spring bean which is using class level annotation + // To name a bean spring gets the class name and converts the first letter to lowercase. + // Default naming strategy of the spring bean which is using class level annotation @Test void givenLoggingServiceBeanIsCreated_whenThereIsNoValueProvided_thenBeanNameShouldBeDefaultName() { assertNotNull(context.getBean("loggingService")); } - // In this case, to name a bean spring gets the class name and converts the first letter to lowercase. + // In this case, to name a bean spring gets the class name and converts the first letter to lowercase. @Test void givenAuditServiceBeanIsCreatedWithMethodLevelAnnotation_whenThereIsNoValueProvided_thenBeanNameShouldBeTheNameOfMethod() { assertNotNull(context.getBean("audit")); } - // To name a bean spring gets the class name and converts the first letter to lowercase. - // Default naming strategy of the spring bean which is using class level annotation + // To name a bean spring gets the class name and converts the first letter to lowercase. + // Default naming strategy of the spring bean which is using class level annotation @Test void givenLoggingGatewayBeanIsCreatedWithFieldLevelAnnotation_whenThereIsNoValueProvided_thenBeanNameShouldBeDefaultName() { assertNotNull(context.getBean("loggingGateway")); } - // spring will create the bean of type CustomComponent with the name "myBean". - // As we're explicitly giving the name to the bean, spring will use this name to refer to it. + // spring will create the bean of type CustomComponent with the name "myBean". + // As we're explicitly giving the name to the bean, spring will use this name to refer to it. @Test void givenCustomComponentBeanIsCreate_whenThereIsCustomNameGivenToBean_thenBeanShouldBeIdentifiedByThatName() { assertNotNull(context.getBean("myBean")); From bb1dcf7ce30784aab3e483325f833ab209930aa0 Mon Sep 17 00:00:00 2001 From: vishal Date: Sun, 11 Apr 2021 18:40:15 +0200 Subject: [PATCH 4/4] BAEL-4785 | Refactor | remove unused code --- .../baeldung/springbean/naming/gateway/LoggingGateway.java | 7 ------- .../baeldung/springbean/naming/service/LoggingService.java | 5 ----- .../springbean/naming/SpringBeanNamingUnitTest.java | 7 ------- 3 files changed, 19 deletions(-) delete mode 100644 spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java deleted file mode 100644 index f2157a5eeb..0000000000 --- a/spring-core-5/src/main/java/com/baeldung/springbean/naming/gateway/LoggingGateway.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.springbean.naming.gateway; - -import org.springframework.stereotype.Service; - -@Service -public class LoggingGateway { -} diff --git a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java index 9276706f7f..3be4e9a028 100644 --- a/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java @@ -1,12 +1,7 @@ package com.baeldung.springbean.naming.service; -import com.baeldung.springbean.naming.gateway.LoggingGateway; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class LoggingService { - - @Autowired - private LoggingGateway gateway; } diff --git a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java index a942897cdd..cccfe22e34 100644 --- a/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java +++ b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java @@ -38,13 +38,6 @@ public class SpringBeanNamingUnitTest { assertNotNull(context.getBean("audit")); } - // To name a bean spring gets the class name and converts the first letter to lowercase. - // Default naming strategy of the spring bean which is using class level annotation - @Test - void givenLoggingGatewayBeanIsCreatedWithFieldLevelAnnotation_whenThereIsNoValueProvided_thenBeanNameShouldBeDefaultName() { - assertNotNull(context.getBean("loggingGateway")); - } - // spring will create the bean of type CustomComponent with the name "myBean". // As we're explicitly giving the name to the bean, spring will use this name to refer to it. @Test