I have a test class:
@SpringBootTest
@AutoConfigureCache(cacheProvider = CacheType.SIMPLE)
class MyTestClass{
@Autowired MyCacheService cacheService;
The bean is annotated:
@Service
@ConditionalOnBean(CacheManager.class)
class MyCacheService
Running the test fails with No qualifying bean of type 'MyCacheService' available
.
It seems that during startup my bean is registered (and conditional evaluated) before the SimpleCacheConfiguration
is evaluated that creates the CacheManager
bean.
Spring Boot 2.4.4
Comment From: snicoll
It seems that during startup my bean is registered (and conditional evaluated) before the SimpleCacheConfiguration is evaluated that creates the CacheManager bean.
@cdalexndr this is the expected behavior and @ConditionalOnBean
should not be used on user-configuration. The Javadoc for this annotation states:
The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.