- 首先实现AutoConfigurationImportFilter接口
public class TfgpOnlineConfigurationFilter implements AutoConfigurationImportFilter {
@Override
public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {
boolean[] res = new boolean[autoConfigurationClasses.length];
Arrays.fill(res, true);
for (int i = 0; i < autoConfigurationClasses.length; i++) {
if (Objects.equals(autoConfigurationClasses[i], "org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration")) {
res[i] = false;
}
}
return res;
}
}
我这里想排除Spring自带的Kafaka配置,想优先加载自己的Kafaka配置
- 在该接口所在resources.META_INFO下的spring.factories文件中添加
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
io.xxxx.xxxx.xxxx.TfgpOnlineConfigurationFilter
总结
如果你想排除某些类,你不想让其加入到ioc容器中,可以采取这个方法主动排除掉你想排除的类