自定义AutoConfigurationImportFilter过滤springboot自动配置类
文章目录
一、前言
springboot环境下,使用rabbitmq,引入了rabbitmq相关的类库,但是又不想使用
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
自动配置类,打算从数据库获取rabbitmq配置信息并通过编程的方式连接rabbitmq;该怎样禁用RabbitAutoConfiguration
自动配置类?
你当然可以通过以下方式禁用禁用RabbitAutoConfiguration
自动配置类
- 方式1-启动类
@EnableAutoConfiguration(exclude=RabbitAutoConfiguration.class)
- 方式2-application.yml
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
但如果当前的模块时公共模块,以上两种方法可能就不太好用了!你可以尝试下自定义
AutoConfigurationImportFilte
二、方案
1.CustomAutoConfigurationImportFilter
package com.example.core.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter;
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
import java.util.Arrays;
import java.util.List;
/**
* 功能描述:通过全限类名过滤自动配置类
*/
public class CustomAutoConfigurationImportFilter implements AutoConfigurationImportFilter, BeanFactoryAware, BeanClassLoaderAware {
public static final Logger log = LoggerFactory.getLogger(CustomAutoConfigurationImportFilter.class);
/**
* 待排除的自动配置类
*/
private static final List<String> EXCLUDE_AUTO_CONFIGURATION_CLASSES =
Arrays.asList("org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration"
, "org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration");
private BeanFactory beanFactory;
private ClassLoader beanClassLoader;
/**
* @param autoConfigurationClasses
* @param autoConfigurationMetadata
* @return
* @see org.springframework.boot.autoconfigure.AutoConfigurationImportSelector
* 所有AutoConfigurationImportFilter匹配结果均为true才会启用自动配置类 org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.ConfigurationClassFilter#filter(java.util.List)
*/
@Override
public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {
boolean[] match = new boolean[autoConfigurationClasses.length];
for (int i = 0; i < autoConfigurationClasses.length; i++) {
// 数组中autoConfigurationClasses可能存在null,注意避空
if (EXCLUDE_AUTO_CONFIGURATION_CLASSES.contains(autoConfigurationClasses[i])) {
match[i] = false;
log.info("已排除自动配置类=>{},如需再次引入,可使用@Import引入!", autoConfigurationClasses[i]);
} else {
match[i] = true;
}
}
return match;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public ClassLoader getBeanClassLoader() {
return beanClassLoader;
}
}
2.spring.factories
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
com.example.core.config.CustomAutoConfigurationImportFilter
3.RabbitMqConfig
package com.example.rabbitmq.config;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* 功能描述:rabbitmq自动配置类
*/
@Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@ConditionalOnProperty(prefix = "spring.rabbitmq", value = "addresses")
@Import(RabbitAutoConfiguration.class)
public class RabbitMqConfig {
}