diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/EmployeeApplication.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/EmployeeApplication.java new file mode 100644 index 0000000000..17c7af858b --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/EmployeeApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.annotations; + +import com.baeldung.annotations.componentscanautoconfigure.teacher.Teacher; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.ComponentScan; + +@ComponentScan(basePackages = {"com.baeldung.annotations.componentscanautoconfigure.healthcare", + "com.baeldung.annotations.componentscanautoconfigure.employee"}, + basePackageClasses = Teacher.class) +@EnableAutoConfiguration(exclude = {JdbcTemplateAutoConfiguration.class}) +//@EnableAutoConfiguration(excludeName = {"org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration"}) +public class EmployeeApplication { + public static void main(String[] args) { + SpringApplication.run(EmployeeApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/employee/Employee.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/employee/Employee.java new file mode 100644 index 0000000000..9be3388046 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/employee/Employee.java @@ -0,0 +1,12 @@ +package com.baeldung.annotations.componentscanautoconfigure.employee; + +import org.springframework.stereotype.Component; + +@Component("employee") +public class Employee { + + @Override + public String toString() { + return "Employee" + this.hashCode(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/employee/SeniorEmployee.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/employee/SeniorEmployee.java new file mode 100644 index 0000000000..242e84f887 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/employee/SeniorEmployee.java @@ -0,0 +1,12 @@ +package com.baeldung.annotations.componentscanautoconfigure.employee; + +import org.springframework.stereotype.Component; + +@Component +public class SeniorEmployee { + + @Override + public String toString() { + return "Senior Employee" + this.hashCode(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/healthcare/Doctor.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/healthcare/Doctor.java new file mode 100644 index 0000000000..7e7e6cb03b --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/healthcare/Doctor.java @@ -0,0 +1,9 @@ +package com.baeldung.annotations.componentscanautoconfigure.healthcare; + +public class Doctor { + + @Override + public String toString() { + return "Doctor" + this.hashCode(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/healthcare/Hospital.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/healthcare/Hospital.java new file mode 100644 index 0000000000..0711544060 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/healthcare/Hospital.java @@ -0,0 +1,13 @@ +package com.baeldung.annotations.componentscanautoconfigure.healthcare; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class Hospital { + + @Bean("doctor") + public Doctor getDoctor() { + return new Doctor(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/student/Student.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/student/Student.java new file mode 100644 index 0000000000..56f2ac9830 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/student/Student.java @@ -0,0 +1,12 @@ +package com.baeldung.annotations.componentscanautoconfigure.student; + +import org.springframework.stereotype.Component; + +@Component("student") +public class Student { + + @Override + public String toString() { + return "Student" + this.hashCode(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/teacher/Teacher.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/teacher/Teacher.java new file mode 100644 index 0000000000..e2c653204d --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/componentscanautoconfigure/teacher/Teacher.java @@ -0,0 +1,12 @@ +package com.baeldung.annotations.componentscanautoconfigure.teacher; + +import org.springframework.stereotype.Component; + +@Component("teacher") +public class Teacher { + + @Override + public String toString() { + return "Teacher" + this.hashCode(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/test/com.baeldung.annotations/EmployeeApplicationTest.java b/spring-boot-modules/spring-boot-annotations/src/main/test/com.baeldung.annotations/EmployeeApplicationTest.java new file mode 100644 index 0000000000..66700cf781 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/test/com.baeldung.annotations/EmployeeApplicationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.annotations; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class EmployeeApplicationTest { + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withUserConfiguration(EmployeeApplication.class); + + @Test + void whenApplicationContextRuns_thenContainAllDefinedBeans() { + contextRunner.run(context -> assertAll( + () -> assertTrue(context.containsBeanDefinition("employee")), + () -> assertTrue(context.containsBeanDefinition("seniorEmployee")), + () -> assertTrue(context.containsBeanDefinition("doctor")), + () -> assertTrue(context.containsBeanDefinition("hospital")), + () -> assertFalse(context.containsBeanDefinition("student")), + () -> assertTrue(context.containsBeanDefinition("teacher")))); + } +}