
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; }}
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年03月28日 03时51分10秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
利用Python实现循环队列
2021-05-08
利用递归实现二叉树的前中后序遍历(Python)
2021-05-08
冒泡排序又来啦(C/C++版本)
2021-05-08
python负数存储
2021-05-08
求二维数组中最大值的位置
2021-05-08
python中sort和sorted的区别
2021-05-08
maven安装
2021-05-08
合并两个有序数组
2021-05-08
聊聊我的五一小假期
2021-05-08
面向对象之异常处理:多路捕获
2021-05-08
Vue新建项目——页面初始化
2021-05-08
Node.js包使用系列(一)——修改NPM全局下载和缓存路径
2021-05-08
TDengine使用(一)——TDengine下载与安装
2021-05-08
6.14编一个程序,将两个字符串s1和s2比较,不要用strcmp函数。
2021-05-08
Java纯文本文件显示工具制作
2021-05-08
Unity2D Fixed Joint 2D详解
2021-05-08
三、案例:留言板 & url.parse()
2021-05-08
Python实验26:计算文件MD5值
2021-05-08
LeetCode:28. 实现 strStr()——————简单
2021-05-08