Spring注解@ComponentScan
一、@ComponentScan注解
1、@ComponentScan注解作用
@ComponentScan用于扫描指定包下的类,将标注有@Controller、@Service、@Repository、@Component4个注解其中一个的类扫描到Spring容器,作为Spring
Bean
2、@ComponentScan使用实例
(1)项目结构
(2)标注有标注有@Controller、@Service、@Repository、@Component4个注解其中一个的类
@Controller
public class Book {
}
@Service
public class Persion {
}
@Repository
public class Student {
}
@Component
public class User {
}
(3)标注有@ComponentScan注解的配置类
@Configuration
/**
* @ComponentScan不指定value的话,默认扫描当前配置类所在的包
*/
@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) {
/**
* java配置类包扫描
*/
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
String[] beanDefinitionNames2 = annotationConfigApplicationContext.getBeanDefinitionNames();
for (String name : beanDefinitionNames2) {
System.out.println(name);
}
}
}
(5)测试结果
二、@ComponentScan注解扫描规则
根据参数includeFilters指定包含扫描哪些组件
根据参数excludeFilters指定排除扫描哪些组件
三、排除excludeFilters
1、常用排除规则
//按照注解来指定规则
FilterType.ANNOTATION
//按照给定的类型来指定规则
FilterType.ASSIGNABLE_TYPE
2、排除案例
(1)标注有@ComponentScan注解的配置类,并指定两个常用的排除规则
@Configuration
/**
* @ComponentScan不指定value的话,默认扫描当前配置类所在的包
*/
@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的话,默认扫描当前配置类所在的包
*/
@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表达式
*/
ASPECTJ,
/**
正则表达式
*/
REGEX,
/**
自定义规则
*/
CUSTOM
}
六、自定义规则
/** Filter candidates using a given custom
* {@link org.springframework.core.type.filter.TypeFilter} implementation.
*/
CUSTOM
自定义规则必须是TypeFilter的实现类
1、项目结构
2、创建自定义规则类 MyTypeFilter
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 {
/**
*
* @param metadataReader 读取到当前正在扫描类的信息
* @param metadataReaderFactory 可以获取到其他任何类的信息
* @return
* @throws IOException
*/
@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的话,默认扫描当前配置类所在的包
*/
@ComponentScan(value = "com.dashu",
includeFilters = {
//自定义规则
@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})},
//禁用掉默认的扫描规则
useDefaultFilters = false)
public class BeanConfig {
}
4、测试结果