栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring注解@ComponentScan

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring注解@ComponentScan

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 = "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注解扫描规则

根据参数includeFilters指定包含扫描哪些组件

根据参数excludeFilters指定排除扫描哪些组件

三、排除excludeFilters 1、常用排除规则
//按照注解来指定规则
FilterType.ANNOTATION
//按照给定的类型来指定规则
FilterType.ASSIGNABLE_TYPE
2、排除案例 (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

自定义规则必须是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 {

    
    @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、测试结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号