我有一個HSMService.java
類,其中有一個ping()
方法允許ping HSM
。在HealthIndicator注入服務時出錯
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());
}
}
我想在一個類中注入這種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);
}
}
根錯誤是:
所致:org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到符合條件的bean [com.app.ddd.services.HSMService]:預計至少有1個符合自動導向候選人的bean。依賴註解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
其中是HSMService類型的bean? – Jobin
添加HSMService的代碼以及軟件包名稱中的軟件包名稱 – developer
com.app.ddd.services – Denis