过滤类型有如下五种:
FilterType type() default FilterType.ANNOTATION;
根据注解进行刷选并不使用默认值刷选出,controller的bean
package com.enjoy.cap2.MainConfiguer;
import com.enjoy.cap1.Person;
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;
@Configuration
@ComponentScan(value = "com.enjoy.cap2",includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
},useDefaultFilters = false)//扫描组件
public class MainConfig {
@Bean
public Person person1(){
return new Person("张三",22);
}
}
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);
}
}
}
输出结果
controller打印出来了但是其他的@service和@Repository没有打印出来
排除的时候 需要excludeFilters 和useDefaultFilters = true,
package com.enjoy.cap2.MainConfiguer;
import com.enjoy.cap1.Person;
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",excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
},useDefaultFilters = true)//扫描组件
public class MainConfig {
@Bean
public Person person1(){
return new Person("张三",22);
}
}
test的结果 并没有打印出controller
指定排除一个类:
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",excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {OderController.class})
},useDefaultFilters = true)//扫描组件
public class MainConfig {
@Bean
public Person person1(){
return new Person("张三",22);
}
}
打印结果没有这个类
指定添加一个类 注意useDefaultFilters = false
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.ASSIGNABLE_TYPE,classes = {OderController.class})
},useDefaultFilters = false)//扫描组件
public class MainConfig {
@Bean
public Person person1(){
return new Person("张三",22);
}
}
注意打印结果有oderController但是没有其他的bean