[英]Spring Boot @ConditionalOnProperty doesn't load bean
I'm trying to implement a service, which will use two different repositories based on env
variable.我正在尝试实现一个服务,它将使用基于
env
变量的两个不同的存储库。
Based on this article: https://reflectoring.io/dont-use-spring-profile-annotation/基于这篇文章: https : //reflectoring.io/dont-use-spring-profile-annotation/
I want to use configuration with @ConditionalOnProperty
annotation to load appropriate implementation on startup.我想使用带有
@ConditionalOnProperty
注释的配置在启动时加载适当的实现。
I did everything exactly like in the article, but spring throws an exception saying that the bean is not defined:我所做的一切都与文章中完全一样,但是 spring 抛出了一个异常,说 bean 未定义:
Description:
Parameter 0 of method myService in com.adam.MyServiceConfiguration required a bean of type 'com.adam.MyRepository' that could not be found.
Action:
Consider defining a bean of type 'com.adam.MyRepository' in your configuration.
Process finished with exit code 0
Here is the configuration of service:下面是服务的配置:
@Configuration
class MyServiceConfiguration {
@Bean
fun mySerivce(myRepository: MyRepository) = MyService(myRepository)
}
And here is the configuration of repository beans:这是存储库bean的配置:
@Configuration
class DataSourceConfiguration {
@Bean
@ConditionalOnProperty(
prefix = "datasource",
name = ["mock"],
havingValue = "false",
matchIfMissing = true
)
fun myRepository(httpClient: HttpClient): MyRepository =
HttpRepository(httpClient)
@Bean
@ConditionalOnProperty(
prefix = "datasource",
name = ["mock"],
havingValue = "true"
)
fun myRepository(): MyRepository = InMemoryRepository()
}
Can anybody see what am I doing wrong?谁能看到我做错了什么? I'm struggling with it for hours.
我为此苦苦挣扎了几个小时。
I tried using @DependsOn but then the error was that there is no bean defined of that name.我尝试使用 @DependsOn 但后来的错误是没有定义该名称的 bean。
I also enabled logging and if I'm reading this right the bean should be resolved properly:我还启用了日志记录,如果我读对了,应该正确解析 bean:
My#repository:
Did not match:
- @ConditionalOnProperty (datasource.mock=false) found different value in property 'mock' (OnPropertyCondition)
Matched:
- @ConditionalOnProperty (datasource.mock=true) matched (OnPropertyCondition)
You have two @Bean
methods named myRepository
on one @Configuration
class.您在一个
@Configuration
类上有两个名为myRepository
@Bean
方法。 This means that there are two different methods both defining a bean named myRepository
.这意味着有两种不同的方法都定义了一个名为
myRepository
的 bean。 This confuses Spring Framework's condition evaluation as described in this issue .这会混淆 Spring Framework 的条件评估,如本期所述。 The confusion means that the conditions on both
myRepository()
methods are evaluated when defining a single myRepository
bean.混淆意味着在定义单个
myRepository
bean 时评估两个myRepository()
方法的条件。 Your two conditions are mutually exclusive so no bean is defined.您的两个条件是互斥的,因此没有定义 bean。
You can fix the problem by ensuring that your two @Bean
methods have distinct names.您可以通过确保您的两个
@Bean
方法具有不同的名称来解决该问题。 For example you could name them httpRepository
and inMemoryRepository
.例如,您可以将它们命名为
httpRepository
和inMemoryRepository
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.