@ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.
Example codes:
animals/Cat.java
@Component
public class Cat {
}
animals/Dog.java
@Component
public class Dog {
}
flowers/Rose.java
@Component
public class Rose {
}
SpringComponentScanApp.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @description
*/
@Configuration
@ComponentScan(basePackages = "com.example.demo.flowers")
public class SpringComponentScanApp {
private static ApplicationContext applicationContext;
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
public static void main(String[] args) {
applicationContext =
new AnnotationConfigApplicationContext(SpringComponentScanApp.class);
System.out.println("------Shark Begin------");
for (String beanName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}