@ComponentScan Without Arguments
In a Spring Application
Use the @ComponentScan
annotation along with the @Configuration
annotation to specify the packages that we want to be scanned.
@ComponentScan
without arguments tells Spring to scan the current package and all of its sub-packages.
package com.componentscan.springapp.animals;
@Component
public class Cat {}
package com.componentscan.springapp.animals;
@Component
public class Dog {}
package com.componentscan.springapp.flowers;
@Component
public class Rose {}
# ------------------------------------------------
package com.componentscan.springapp
@Configuration
@ComponentScan
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);
for (String beanName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}
springComponentScanApp
cat
dog
rose
exampleBean
In a Spring Boot Application
SpringBoot use @SpringBootApplication
annotation, and it’s a combination of three annotations:
@Configuration
@EnableAutoConfiguration
@ComponentScan
package com.componentscan.springbootapp;
@SpringBootApplication
public class SpringBootComponentScanApp {
private static ApplicationContext applicationContext;
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
public static void main(String[] args) {
applicationContext = SpringApplication.run(SpringBootComponentScanApp.class, args);
checkBeansPresence( "cat", "dog", "rose", "exampleBean", "springBootComponentScanApp");
}
private static void checkBeansPresence(String... beans) {
for (String beanName : beans) {
System.out.println("Is " + beanName + " in ApplicationContext: " + applicationContext.containsBean(beanName));
}
}
}
Is cat in ApplicationContext: true
Is dog in ApplicationContext: true
Is rose in ApplicationContext: true
Is exampleBean in ApplicationContext: true
Is springBootComponentScanApp in ApplicationContext: true
这里检验存在而不是全部输出, is that the output would be too large.
This is because of the implicit@EnableAutoConfiguration
annotation, which makes Spring Boot create many beans automatically, relying on the dependencies in pom.xml file.
@ComponentScan With Arguments
In a Spring Application
package com.componentscan.springapp.animals;
@Component
public class Cat {}
package com.componentscan.springapp.animals;
@Component
public class Dog {}
package com.componentscan.springapp.flowers;
@Component
public class Rose {}
@ComponentScan(basePackages = "com.componentscan.springapp.animals")
@Configuration
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);
for (String beanName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}
springComponentScanApp
exampleBean
cat
dog
In a Spring Boot Application
@SpringBootApplication
@ComponentScan(basePackages = "com.componentscan.springbootapp.animals")
@ComponentScan with Multiple Packages
@ComponentScan(basePackages = {"com.componentscan.springapp.animals", "com.componentscan.springapp.flowers"})
# since spring 4.1.1
@ComponentScan(basePackages = "com.componentscan.springapp.animals;com.baeldung.componentscan.springapp.flowers")
@ComponentScan(basePackages = "com.componentscan.springapp.animals,com.baeldung.componentscan.springapp.flowers")
@ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals com.componentscan.springapp.flowers")
@ComponentScan with Exclusions/IncludeFilters
参考:Spring @ComponentScan – Filter Types