本意是去除子模块中的redis连接,但是有更简洁的方式:
spring.redis.enabled=false #关闭依赖的auth-module中的redis连接
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;
/**
* 解释:
* BeanDefinitionRegistryPostProcessor是一个特殊的BeanFactoryPostProcessor,它允许在Spring容器加载Bean定义后,在将这些定义转化为实际的Bean实例之前,对Bean定义进行进一步的修改、添加或删除操作。
* 特点:
* 在Spring容器启动阶段执行:BeanDefinitionRegistryPostProcessor是在Bean定义加载到Spring容器之后,Bean实例化之前调用的。
* 修改Bean定义:可以通过实现该接口来动态修改或增加Bean定义,例如修改Bean的属性值、更改Bean的作用域、添加额外的Bean定义等。
* 扩展Spring功能:通过使用BeanDefinitionRegistryPostProcessor,开发者可以在Spring容器启动过程中插入自定义逻辑,以满足特定需求。
*/
@Component
public class RemoveRegistryBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
String[] names = registry.getBeanDefinitionNames();
for (String name : names) {
// if (name.contains("redisConfig")) {
// registry.removeBeanDefinition(name);
// }
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}