• springboot启动的时候排除加载某些bean


    一、缘起

    由于公司把redis相关的配置类,工具类放在了一个类似common的工程里,这样以后肯定不可避免的出现某些项目可能并不需要使用redis,但是还是依赖common里的别的一些类库

    所以排除springboot启动加载的一些bean还是有意义的

    二、@ComponenScan注解

    @ComponentScan注解用来扫描加排除,不加ComponentScan注解,springboot是默认扫描springboot启动类所在的包及其子包,我们现在自己扫描,然后使用@ComponentScan注解的excludeFilters属性用来排除不想扫描的bean。

    我的示例是排除redis相关的配置,光排除自己写的配置类时不行的,还需要排除springboot提供的自动配置类,使用@SpringBootApplication的exclude属性把RedisAutoConfiguration.class排除掉,这个时候启动,结果尴尬了,报错一个组件依赖了redisTemplate这个bean。找了挺久,原来还要把RedisRepositoriesAutoConfiguration去掉

    代码如下

    @SpringBootApplication(exclude = {RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class})
    @EnableEurekaClient
    @EnableCircuitBreaker
    @EnableDiscoveryClient
    @EnableTransactionManagement
    @ComponentScan(value = "com.aw.phjr", excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {RedisConfiguration.class, RedisUtil.class}))
    public class RestRpc_Application {

    如果想知道配置redis或者其他组件都有哪些自动配置类,可以参考文章:https://www.cnblogs.com/xhy-shine/p/11274906.html

  • 相关阅读:
    阿里云的服务器内网互通的前提条件
    Java Map 接口
    ModelAndView学习笔记
    tomcat错误信息解决方案【严重:StandardServer.await: create[8005]】
    jquery获得select option的值 和对select option的操作
    【Git使用详解】Egit的常用操作详解
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/xhy-shine/p/10718965.html
Copyright © 2020-2023  润新知