我有一个像下面这样的课程
@Configuration
@ConfigurationProperties(prefix = "myConfig")
class MyConfig {
lateinit var baseUrl: String
var connectionTimeout: Int = 2000
var defaultMaxHotels: Int = 10000
var clientId: String = "my-domain"
var applicationId: String = "my-app"
var failureRateThreshold: Float = 20f
var windowSize: Int = 100
var minNumberOfCalls: Int = 100
var waitDurationInOpenState: Long = 30_000
@Bean
fun myCircuitBreaker(): CircuitBreaker {
var configuration = CircuitBreakerConfig.custom().failureRateThreshold(failureRateThreshold)
.slidingWindow(windowSize, minNumberOfCalls, CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
.waitDurationInOpenState(Duration.ofMillis(waitDurationInOpenState))
.enableAutomaticTransitionFromOpenToHalfOpen()
.build()
val registry = CircuitBreakerRegistry.of(configuration)
return registry.circuitBreaker("my-cb")
}
启动我的应用程序时,出现错误提示
An exception occurred while running. null: InvocationTargetException: Found @Configuration and @ConfigurationProperties on com.my.api.config.client.MyConfig.
请帮助我解决问题。
答案 0 :(得分:0)
@Configuration
和@ConfigurationProperties
不能同时应用于一种类型。但是,您可以通过以下两种方式来实现您想要的目标
@ConfigurationProperties
类的@Bean
上使用@Configuration
application.properties
config.circuit-breaker.connection-timeout=2000
config.circuit-breaker.clientId=clientId
CircuitBreaker
public class CircuitBreaker {
private int connectionTimeout;
private String clientId;
// no-args constructor
// getters/setters
}
CircuitBreakerConfiguration
@Configuration
public class CircuitBreakerConfiguration {
@Bean
@ConfigurationProperties(prefix = "config.circuit-breaker")
public CircuitBreaker circuitBreaker() {
return new CircuitBreaker();
}
}
@ConfigurationProperties
和@Configuration
分开 application.properties
config.circuit-breaker.connection-timeout=2000
config.circuit-breaker.clientId=clientId
CircuitBreakerConfigurationProperties
@ConfigurationProperties(prefix = "config.circuit-breaker")
public class CircuitBreakerConfigurationProperties {
private int connectionTimeout;
private String clientId;
// no-args constructor
// getters/setters
}
CircuitBreaker
public class CircuitBreaker {
private int connectionTimeout;
private String clientId;
// no-args constructor
// getters/setters
}
CircuitBreakerConfiguration
@Configuration
public class CircuitBreakerConfiguration {
@Bean
public CircuitBreaker circuitBreaker(
CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties) {
final CircuitBreaker circuitBreaker = new CircuitBreaker();
circuitBreaker.setClientId(circuitBreakerConfigurationProperties.getClientId());
circuitBreaker
.setConnectionTimeout(circuitBreakerConfigurationProperties.getConnectionTimeout());
return circuitBreaker;
}
}