2、@ComponentScan使用实例 (1)项目结构 (2)标注有标注有@Controller、@Service、@Repository、@Component4个注解其中一个的类@ComponentScan用于扫描指定包下的类,将标注有@Controller、@Service、@Repository、@Component4个注解其中一个的类扫描到Spring容器,作为Spring
Bean
@Controller
public class Book {
}
@Service
public class Persion {
}
@Repository
public class Student {
}
@Component
public class User {
}
(3)标注有@ComponentScan注解的配置类
@Configuration
@ComponentScan(value = "com.dashu")
public class BeanConfig {
}
(4)测试类
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
String[] beanDefinitionNames2 = annotationConfigApplicationContext.getBeanDefinitionNames();
for (String name : beanDefinitionNames2) {
System.out.println(name);
}
}
}
(5)测试结果
二、@ComponentScan注解扫描规则
三、排除excludeFilters 1、常用排除规则根据参数includeFilters指定包含扫描哪些组件
根据参数excludeFilters指定排除扫描哪些组件
//按照注解来指定规则 FilterType.ANNOTATION //按照给定的类型来指定规则 FilterType.ASSIGNABLE_TYPE2、排除案例 (1)标注有@ComponentScan注解的配置类,并指定两个常用的排除规则
@Configuration
@ComponentScan(value = "com.dashu",excludeFilters = {
//按照注解的方式,排除标有注解@Controller的类
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),
//按照指定类型,排除Persion类
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {Persion.class})
})
public class BeanConfig {
}
(2) 测试结果
四、指定包含includeFilters
1、常用包含规则
//按照注解来指定规则
FilterType.ANNOTATION
//按照给定的类型来指定规则
FilterType.ASSIGNABLE_TYPE
//使用指定包含规则,需要禁用掉默认的扫描规则
useDefaultFilters = false
2、指定包含案例
(1)标注有@ComponentScan注解的配置类,并指定两个常用的指定包含规则
@Configuration
@ComponentScan(value = "com.dashu",
includeFilters = {
//按照注解的方式,排除标有注解@Controller的类
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),
//按照指定类型,排除Persion类
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {Persion.class})},
//禁用掉默认的扫描规则
useDefaultFilters = false)
public class BeanConfig {
}
(2) 测试结果
五、FilterType规则
public enum FilterType {
ANNOTATION,
ASSIGNABLE_TYPE,
ASPECTJ,
REGEX,
CUSTOM
}
六、自定义规则
CUSTOM
1、项目结构 2、创建自定义规则类 MyTypeFilter自定义规则必须是TypeFilter的实现类
import org.springframework.core.io.Resource;
import org.springframework.core.type.Annotationmetadata;
import org.springframework.core.type.Classmetadata;
import org.springframework.core.type.classreading.metadataReader;
import org.springframework.core.type.classreading.metadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import java.io.IOException;
public class MyTypeFilter implements TypeFilter {
@Override
public boolean match(metadataReader metadataReader, metadataReaderFactory metadataReaderFactory) throws IOException {
//获取当前正在扫描的类的注解信息
Annotationmetadata annotationmetadata = metadataReader.getAnnotationmetadata();
//获取当前正在扫描的类的类信息
Classmetadata classmetadata = metadataReader.getClassmetadata();
//获取当前正在扫描的类的资源信息
Resource resource = metadataReader.getResource();
//获取当前类名
String className = classmetadata.getClassName();
//判断当前类名是否包含Persion,包含Persion则返回true,将该类扫描到容器,否则返回false过滤
if (className.indexOf("Persion") != -1){
return true;
}
return false;
}
}
3、标注有@ComponentScan注解的配置类,并指定自定义规则
org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(value = "com.dashu",
includeFilters = {
//自定义规则
@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})},
//禁用掉默认的扫描规则
useDefaultFilters = false)
public class BeanConfig {
}
4、测试结果



