gpt4 book ai didi

java - 在 HealthIndicator 中注入(inject)服务时出错

转载 作者:行者123 更新时间:2023-12-01 09:13:29 24 4
gpt4 key购买 nike

我有一个HSMService.java其中有一个类 ping()允许 ping HSM 的方法。

package com.app.ddd.services;

import com.app.ddd.messages.EchoRequest;

public class HSMService implements HSMServiceI {

private SynchronousEstablishedConnection connection;
private String genericGroupName;

public HSMService(EstablishedConnection connection, String genericGroupName) {
this.connection = new SynchronousEstablishedConnection(connection);
this.genericGroupName = genericGroupName;
}

@Override
public void ping() {
connection.submit(new EchoRequest());
}
}

我想注入(inject)这个HSMService在实现 HealthIndicator 的类中:

HSMHealthIndicator.java:

@Component
public class HSMHealthIndicator implements HealthIndicator {

@Autowired
private HSMService hsmService;

private String host;
private int port;

private int checkHSMStatus() {

//just to test
if (hsmService == null)
System.out.println("hsmService null");
return 0;
}

@Override
public Health health() {

if (checkHSMStatus() != 0) {
return Health.down().withDetail("Error Code", checkRKMSStatus()).build();
}
return Health.up().build();
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public HSMService getHSMService() {
return hsmService;
}

public void setHSMService(HSMService hsmService) {
this.hsmService= hsmService;
}

}

此类由 HSMEndpoint.java 使用实现 org.springframework.boot.actuate.endpoint.Endpoint 的类

HSMEndpoint.java 摘录:

@Override
public String invoke() {

HSMHealthIndicator h = new HSMHealthIndicator();
h.setHost(this.getHost());
h.setPort(this.getPort());
Status s = h.health().getStatus();
return "Status of the HSM : " + s.getCode();
}

最后 HSMEndpoint.java 由类 HSMEndpointConfiguration.java 进行配置:

@Configuration
public class HSMEndpointConfiguration{

@Bean
//The value of hsm.host and the value of hsm.port are in application.properties
public Endpoint getHSMEndpoint(@Value("${hsm.host}")String host, @Value("${hsm.port}")int port) {
return new HSMEndpoint(host, port);
}
}

根本错误是:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.app.ddd.services.HSMService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

最佳答案

将以下行添加到 HsmService 类..

@Service("hsmService")

所以就变成了..

@Service("hsmService")
public class HSMService implements HSMServiceI {

关于java - 在 HealthIndicator 中注入(inject)服务时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40763740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com