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

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

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

首先由自己配置的RedisConfiuration类,还有RedisUtil类,可以使用@ComponentScan注解用来扫描加排除,不加ComponentScan注解,springboot是默认扫描springboot启动类所在的包及其子包,我们现在自己扫描,然后使用@ComponentScan注解的excludeFilters属性用来排除不想扫描的bean

然后自己的配置没有了,springboot还会加载他自己默认的redis配置类,@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 {

猜你喜欢

转载自www.cnblogs.com/xhy-shine/p/10718965.html