I have a configuration class, let’s call it TestConfig
.
It is annotated with:
@RefreshScope
@Configuration
@RequiredArgsConstructor
@Slf4j
public class TestConfig implements ApplicationListener<EnvironmentChangeEvent>
While trying to run my application locally or running test classess annotated with @SpringBootTest
(which load the whole application context), it throws conflicting bean exception:
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'testConfig ' for bean class [com.service.config.test.TestConfig]
conflicts with existing, non-compatible bean definition of same name and class [com.service.config.test.TestConfig]
It looks like the bean is conflicting with itself because of the @RefreshScope
annotation.
The test class I tried to run:
@SpringBootTest(
properties = {"spring.main.allow-bean-definition-overriding=true"},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
public class BigBangTest
I already ensured that there is no other bean with the same name and tried to add properties = {"spring.main.allow-bean-definition-overriding=true"}
to my @SpringBootTest
property, but the same exception still thrown.
I also tried to remove the @RefreshScope
annotation from TestConfig
and it works, no ConflictingBeanDefinitionException
was thrown.
My question is, how to solve this while keeping the TestConfig
annotated with @RefreshScope
?
1