@ComponentScan-组件扫描配置
excludeFilters 排除那些组件
配置类
// 配置类注解 告诉spring这是个配置 @Configuration // excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件 // FilterType.ANNOTATION 按照注解方式排除 @ComponentScans(value = { @ComponentScan(value = "com.example.studywork.work",excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class}) }) }) public class MyConfig { // 给容器注册一个bean,返回类型是方法返回值类型,beanName就是方法名称 @Bean public Pet configName(){ return new Pet("小猫",2); } }
输出
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); String[] beanNamesForType = applicationContext.getBeanDefinitionNames(); for (String name : beanNamesForType) { System.out.println(name); }
includeFilters 包含哪些组件
配置类
// 配置类注解 告诉spring这是个配置 @Configuration //includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件 // useDefaultFilters 默认是true 指示是否应启用对使用 @Component @Repository、@Service 或 @Controller 注释的类的自动检测 false 手动 @ComponentScans(value = { @ComponentScan(value = "com.example.studywork.work",includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) },useDefaultFilters = false) }) public class MyConfig { // 给容器注册一个bean,返回类型是方法返回值类型,beanName就是方法名称 @Bean public Pet configName(){ return new Pet("小猫",2); } }
输出
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); String[] beanNamesForType = applicationContext.getBeanDefinitionNames(); for (String name : beanNamesForType) { System.out.println(name); }
扩展
注意:
如果 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
写在test中,扫描路径value = "com.example.studywork.work" 路径中一定不要有@SpringBootApplication 主程序类
现在下载框架直接生成出主程序 一定要注意
你可以把 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
写在主程序类中启动项目查看输出
用对使用 @Component @Repository、@Service 或 @Controller 注释的类的检测
FilterType.ANNOTATION:按照注解
FilterType.ASSIGNABLE_TYPE:按照给定的类型;
FilterType.ASPECTJ:使用ASPECTJ表达式
FilterType.REGEX:使用正则指定
FilterType.CUSTOM:使用自定义规则
写多个@ComponentScan注解可以用一下两种方法
@Repeatable(ComponentScans.class)
@ComponentScans(value = {})
FilterType.ASSIGNABLE_TYPE:按照给定的类型;
FilterType.CUSTOM:使用自定义规则
先创建一个类实现TypeFilter接口
public class MyTypeFilter implements TypeFilter { /** * metadataReader – 读取到的当前正在扫描的类的信息 * metadataReaderFactory – 可以获取到其他任何类信息的 * * @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();
// 这里可以写我们的业务逻辑 判断是否注入到容器中 // 判断类名是否有 Se if(className.contains("Se")){ // 有 则返回真 返回真是让有Se的类注入容器中 return true; } return false; } }
// 配置类注解 告诉spring这是个配置 @Configuration @ComponentScans(value = { @ComponentScan(value = "com.example.studywork.work",includeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class}) },useDefaultFilters = false) }) public class MyConfig { // 给容器注册一个bean,返回类型是方法返回值类型,beanName就是方法名称 @Bean public Pet configName(){ return new Pet("小猫",2); } }
输出
类名带有Se的只有myService