Merge pull request #10098 from anirban99/feature/bael-4334/ComponentScan-and-EnableAutoConfiguration-in-Spring-Boot
BAEL-4334: ComponentScan and EnableAutoConfiguration in Spring Boot
This commit is contained in:
commit
e407389149
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.annotations.componentscanautoconfigure.healthcare;
|
||||
|
||||
public class Doctor {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Doctor" + this.hashCode();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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"))));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user