I have created the following configuration (I duplicated the @Lazy because I was not able to make it work)
@Lazy(true)
@Configuration
public class ElasticConfiguration
{
private Logger logger = LoggerFactory.getLogger(ElasticConfiguration.class);
@Value("${elasticsearch.host}")
private String esHost;
@Value("${elasticsearch.port}")
private int esPort;
@Value("${elasticsearch.clustername}")
private String esClusterName;
@Lazy(true)
@Bean
public Client client() throws Exception
{
Builder builder = Settings.builder();
// builder.put("client.transport.sniff", true);
Settings settings = builder.put("cluster.name", esClusterName).build();
TransportClient client = new PreBuiltTransportClient(settings);
InetAddress adress = InetAddress.getByName(esHost);
client.addTransportAddress(new InetSocketTransportAddress(adress, esPort));
logger.debug(
"ES Client built with cluster.name;" + esClusterName + ";host;" + esHost + ";port;" + esPort + ";client.transport.sniff;"
+ builder.get("client.transport.sniff"));
return client;
}
@Lazy(true)
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception
{
return new ElasticsearchTemplate(client());
}
}
I am not able to have the lazy initialization because it seems my configuration is overriden by spring-boot auto configuration
Overriding bean definition for bean 'elasticsearchTemplate' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=true; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=elasticConfiguration; factoryMethodName=elasticsearchTemplate; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [xxx/xxx/ElasticConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration; factoryMethodName=elasticsearchTemplate; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]]
I also tried to add the following annotation on the class
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
in association with the following property, but it did not work better
org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.xxx.ElasticConfiguration
What should I do ?