Denis Denis - 1 year ago 77
Java Question

Error when injecting a service in a HealthIndicator

I have a
class in which there is a
method allowing to ping a



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;

public void ping() {
connection.submit(new EchoRequest());

I want to inject this
in a class that implements a
: :

public class HSMHealthIndicator implements HealthIndicator {

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;

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) { = 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
class which implements

Excerpt of :

public String invoke() {

HSMHealthIndicator h = new HSMHealthIndicator();
Status s =;
return "Status of the HSM : " + s.getCode();

Finally is configured by the class :

public class HSMEndpointConfiguration{

//The value of and the value of hsm.port are in
public Endpoint getHSMEndpoint(@Value("${}")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 []: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Answer Source

The sole solution I found : :

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 =;
       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...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download