我们知道在springboot中有这么一个自动配置的功能:假如你在工程中加入某个jar,比如mysql的jar,或者mongodb的jar,或者redis的jar,那么就算不写配置文件,springbboot也会启动相关的功能来简化构建这些客户端的复杂度。
但是有那么一种场景,比如我只想在工程中使用某个jar的某个类的功能,所以我必须要引入某个jar,但是又不想让springboot自动配置,那该怎么办?
其实很简单,只需要在启动类上进行配置,如下用mongodb来举例:
@SpringBootApplication(exclude={MongoAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这个时候来问题了,我假如想不包含 redis 的自动配置,那该如何写这个 XXXConfiguration.class 呢,这个到哪里找?
在回答这个问题之前,我们先来看一个错误提示,以redis的自动配置来举例,当设置不正确的时候,springboot启动的时候会进行报错,报错的内容大致如下:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled
with ‘debug’ enabled,这个是什么鬼?这个debug在哪里设置啊,难道在 logback 中?不是吧。带着这个问题,我查了资料,找到这篇文章:http://www.it1352.com/908199.html,这篇文章里讲了该如何在springboot中进行设置,哦,原来就是在 application.properties 中假如 debug=true 即可。顺便吐槽一下这篇文章的网站,里面广告太多了,感觉是一个。。。
好了,我们继续上面的那个redis那个例子,我们把 debug=true 设置好,好了,真相出来了:
RedisAutoConfiguration matched:
- @ConditionalOnClass found required classes 'org.springframework.data.redis.connection.jedis.JedisConnection', 'org.springframework.data.redis.core.RedisOperations', 'redis.clients.jedis.Jedis'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
RedisAutoConfiguration.RedisConfiguration#redisTemplate matched:
- @ConditionalOnMissingBean (names: redisTemplate; SearchStrategy: all) did not find any beans (OnBeanCondition)
RedisAutoConfiguration.RedisConfiguration#stringRedisTemplate matched:
- @ConditionalOnMissingBean (types: org.springframework.data.redis.core.StringRedisTemplate; SearchStrategy: all) did not find any beans (OnBeanCondition)
RedisAutoConfiguration.RedisConnectionConfiguration matched:
- @ConditionalOnClass found required class 'org.apache.commons.pool2.impl.GenericObjectPool'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
RedisAutoConfiguration.RedisConnectionConfiguration#redisConnectionFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.data.redis.connection.RedisConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
RedisCacheConfiguration matched:
- Cache org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration automatic cache type (CacheCondition)
RedisRepositoriesAutoConfiguration matched:
- @ConditionalOnClass found required classes 'redis.clients.jedis.Jedis', 'org.springframework.data.redis.repository.configuration.EnableRedisRepositories'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnProperty (spring.data.redis.repositories.enabled=true) matched (OnPropertyCondition)
- @ConditionalOnMissingBean (types: org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean; SearchStrategy: all) did not find any beans (OnBeanCondition)
于是,我们就可以这么设置我们的启动类:
@SpringBootApplication(exclude={RedisRepositoriesAutoConfiguration.class,RedisAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
上面的RedisCacheConfiguration是个私有类,不能用,不用也达到了效果。
全文结束。