Denis Denis - 15 days ago 9
Java Question

Error when injecting a service in a HealthIndicator

I have a

HSMService.java
class in which there is a
ping()
method allowing to ping a
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());
}
}


I want to inject this
HSMService
in a class that implements a
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;
}

}


This class is used by a
HSMEndpoint.java
class which implements
org.springframework.boot.actuate.endpoint.Endpoint


Excerpt of 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();
}


Finally HSMEndpoint.java is configured by the class 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);
}
}


The root error is :


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)}

Answer

The sole solution I found :

Application.java :

@SpringBootApplication
public class Application {

    private static ServiceFactory serviceFactory;

    public static void main(String[] args) {

        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class);
        if(args.length > 1) {
           springApplicationBuilder = springApplicationBuilder.web(false);
    }
       ConfigurableApplicationContext context = springApplicationBuilder.run(args);
       serviceFactory = context.getBean(ServiceFactory.class);

       ...
}

It leads to no error when I run the application. ServiceFactory is a class which does not implement any class and which does not extend any class. When I try to do the same with a Service class which implements an interface, the server crashes... Dunno why...