@CompentScan的作用就是告诉spring ioc哪些哪些包需要被扫描到。
1,basePackages 配置哪些包下的类可以被扫描到。
@Configuration
@ComponentScan(basePackages = {"com.tuling.testcompentscan.controller","com.tuling.testcompentscan.dao"})
public class MainConfig {
}
public class MainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
String[] beanDefinationNames = ctx.getBeanDefinitionNames();
for (String name:beanDefinationNames) {
System.out.println("bean的定义信息:"+name);
}
}
}
因为只配置了扫描controller和dao包,所以只有controller和dao的包下面的配置了@Controller, @Service, @Repository, @Component被扫描到了.
其实@Controller, @Service, @Repository中都包含了@Component注解, 其本质就时扫描有@Component注解的类。
结果如下:
FilterType的种类介绍:
a)注解形式的FilterType.ANNOTATION -- 控制:@Controller @Service @Repository @Component被忽略或加入
b)指定类型的 FilterType.ASSIGNABLE_TYPE -- 指定某些类会被忽略