2016-11-23 47 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