spring自定义组件扫描器
发布日期:2021-05-07 14:38:28 浏览次数:20 分类:原创文章

本文共 1434 字,大约阅读时间需要 4 分钟。

xml格式配置

<context:component-scan base-package="com.itheima">        <context:exclude-filter expression="com.lzb.config.MyFilter" type="custom"/>    </context:component-scan>

注解格式

import org.springframework.beans.factory.annotation.Configurable;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Repository;@Configurable//代表这是一个配置类@ComponentScan(value = "com.lzb",    excludeFilters = @ComponentScan.Filter(    	type = FilterType.CUSTOM,classes = MyTypeFilter.class   	))//扫描包@PropertySource("classpath:jdbc.properties")@Import({   JDBCConfig.class,MybatisConfig.class})public class SpringConfig {   }
接下来就是类过滤器的详解	返回值为true则拦截这个bean的创建。
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 MyFilter implements TypeFilter {       public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {       	//这里查看类可知是bean的具体实现类,这样可以通过各种判断来决定哪些bean不被创建        String className = metadataReader.getClassMetadata().getClassName();        System.out.println(className);        return false;    }}

在这里插入图片描述

上一篇:spring自定义导入器
下一篇:dubbo的初级使用

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年03月28日 03时51分10秒