2016-11-23 72 views
0

我有一個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(所需=真)}

+0

其中是HSMService類型的bean? – Jobin

+0

添加HSMService的代碼以及軟件包名稱中的軟件包名稱 – developer

+0

com.app.ddd.services – Denis

回答

1

添加下列行來HsmService類..

@Service("hsmService") 

所以它成爲..

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

我仍然有錯誤: 創建名爲'hsmService'的bean時定義的錯誤... 通過構造函數參數0表示的不滿足的依賴關係;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有找到符合條件的bean [com.app.ddd.services.EstablishedConnection]:預計至少有1個bean有資格作爲autowire候選者。依賴註釋:{} – Denis

+0

@Denis仍然出錯? – Jobin