1

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 ?

1 Answer 1

1

Ok so the only solution I found was to use the following property

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration

Note that using the folling annotation did not work for me ...

@EnableAutoConfiguration(exclude={ElasticsearchDataAutoConfiguration.class})

EDIT

In fact my problem was that my method elasticsearchTemplate() was returning ElasticsearchOperations instead of ElasticsearchTemplate.

When changing the signature the @Lazy annotation seems to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.