在HealthIndicator中注入服务时出错

时间:2016-11-23 11:49:57

标签: java spring-boot

我有一个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());
    }
}

我想在实现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:找不到依赖项[com.app.ddd.services.HSMService]的限定bean:预计至少有1个bean可以作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

1 个答案:

答案 0 :(得分:1)

将以下行添加到HsmService类..

@Service("hsmService")

所以它变成了......

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