我创建了以下配置(我复制了@Lazy,因为我无法使其工作)

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


我无法进行延迟初始化,因为似乎我的配置被spring-boot自动配置覆盖了


  用a覆盖bean'elasticsearchTemplate'的bean定义
  不同的定义:替换[Root bean:class [null]; scope =;
  abstract = false; lazyInit = true; autowireMode = 3; dependencyCheck = 0;
  autowireCandidate = true; primary = false;
  factoryBeanName = elasticConfiguration;
  factoryMethodName = elasticsearchTemplate; initMethodName = null;
  destroyMethodName =(推断);在类路径资源中定义
  [xxx / xxx / ElasticConfiguration.class]]
  与[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 =(推断);在类路径资源中定义
  [org / springframework / boot / autoconfigure / data / elasticsearch / ElasticsearchDataAutoConfiguration.class]]


我也尝试在类上添加以下注释

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)


与以下属性相关联,但效果不佳

org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.xxx.ElasticConfiguration


我该怎么办 ?

最佳答案

好的,所以我发现的唯一解决方案是使用以下属性

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


请注意,使用以下标注对我不起作用...

@EnableAutoConfiguration(exclude={ElasticsearchDataAutoConfiguration.class})


编辑

实际上,我的问题是我的方法elasticsearchTemplate()返回的是ElasticsearchOperations而不是ElasticsearchTemplate

更改签名时,@ Lazy批注似乎起作用。

10-08 13:11