Karesh A
Karesh A

Reputation: 1751

Spring Boot Custom connection object

In my spring boot application I have the elasticsearch 5.4 transport client connection.

@Configuration
public class EsConfig {

    @Value("${elastic.host}")
    private String esHost;

    @Value("${elastic.port}")
    private int esPort;

    @Bean
    public TransportClient client() throws Exception {

        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esHost), esPort));
        return client;
    }

}

I need to get the instance inside service class to use it in my operation.

@Service
public class ElasticService {

    TransportClient client;

    public String getInfo(){
          client.methodCall() ...
    }

}

How can I autowire the client object defined inside ElasticService.

Upvotes: 0

Views: 61

Answers (1)

Val
Val

Reputation: 217474

How about this?

@Service
public class ElasticService {

    @Autowired                    <---- add this annotation
    TransportClient client;

    public String getInfo(){
    }

}

Upvotes: 1

Related Questions