package com.enjoy.cap2.MainConfiguer;
import com.enjoy.cap1.Person;
import com.enjoy.cap2.controller.OderController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
@Configuration
@ComponentScan(value = "com.enjoy.cap2",includeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MYTyperFilter.class})
},useDefaultFilters = false)//扫描组件
public class MainConfig {
@Bean
public Person person1(){
return new Person("张三",22);
}
}
在5的时候我们知道 useDefaultFilters = true的时候会扫描所有的注解到容器,那么具体是怎么扫描的呢?
我们先看一下测试用例:
package test;
import com.enjoy.cap2.MainConfiguer.MainConfig;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainTEwst {
@Test
public void sss(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
for(String name:beanDefinitionNames){
System.out.println(name);
}
}
}
我么跟一下容器代码。
useDefaultFilters 看到了 如果useDefaultFilters 为true
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
只要包含@Component都会被扫描进容器
然后我们看看看 @Controller@Service@Repository都包含Component所以都会被加载进入容器
这就是在include的时候useDefaultFilters 为true的话 所有都会加载进来。
还可以用eccludefilter进行排除。