Ser*_*tin 3 java spring spring-boot spring-boot-actuator
我想根据应用程序正常运行时间来自定义HealthIndicator。
@Component
public class HealthActuator implements HealthIndicator {
private final MetricsEndpoint metricsEndpoint;
@Autowired
public HealthActuator(MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}
@Override
public Health health() {
long uptime = (Long) metricsEndpoint.invoke().get("uptime");
// logic with uptime
return Health.up().build();
}
}
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:a circular dependency between 2 beans in the application context。
我可以通过对端点/ actuator / health进行休息呼叫来获得正常运行时间指标。
但是也许可以通过编程来实现吗?
PS日志堆栈跟踪:
11-01 14:34:09 WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthActuator' defined in file [/Users/serge/projects/bb/bb-imapl/target/classes/bb/imapl/config/HealthActuator.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$2bb06d4a]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'healthActuator': Requested bean is currently in creation: Is there an unresolvable circular reference?
11-01 14:34:09 WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthActuator' defined in file [/Users/serge/projects/bb/bb-imapl/target/classes/bb/imapl/config/HealthActuator.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$2bb06d4a]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'healthActuator': Requested bean is currently in creation: Is there an unresolvable circular reference?
11-01 14:34:09 ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
There is a circular dependency between 2 beans in the application context:
- healthActuator defined in file [/Users/serge/projects/bb/bb-imapl/target/classes/bb/imapl/config/HealthActuator.class]
- org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
- healthActuator
11-01 14:34:09 ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢二传手注射,也可以使用@Lazy... 解决此问题。
@Autowired
public HealthActuator(@Lazy MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}
Run Code Online (Sandbox Code Playgroud)
EndpointAutoConfiguration依赖于HealthIndicator(您使用 实现HealthActuator)。
所以你最终会得到一个循环依赖。当 2 个 bean 需要彼此实例化自己时(通过构造函数注入),就会发生这种情况。您可以通过使用 setter 注入来打破循环:
@Component
public class HealthActuator implements HealthIndicator {
private MetricsEndpoint metricsEndpoint;
@Autowired
private void setMetricsEndpoint(MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}
public HealthActuator() {
}
@Override
public Health health() {
long uptime = (Long) metricsEndpoint.invoke().get("uptime");
// logic with uptime
return Health.up().build();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2819 次 |
| 最近记录: |