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..8c70078822 --- /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/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..3be4e9a028 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/springbean/naming/service/LoggingService.java @@ -0,0 +1,7 @@ +package com.baeldung.springbean.naming.service; + +import org.springframework.stereotype.Service; + +@Service +public class LoggingService { +} 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..2b7cb84742 --- /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/SpringBeanNamingUnitTest.java b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java new file mode 100644 index 0000000000..cccfe22e34 --- /dev/null +++ b/spring-core-5/src/test/java/com/baeldung/springbean/naming/SpringBeanNamingUnitTest.java @@ -0,0 +1,65 @@ +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; +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 SpringBeanNamingUnitTest { + + 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")); + } + + // 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().getClass()).isEqualTo(Cat.class); + assertThat(petShow.getDog().getClass()).isEqualTo(Dog.class); + } +}