我创建了以下配置(我复制了@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 auto配置覆盖了
覆盖bean的定义bean' elasticsearchTemplate'用一个 不同的定义:替换[Root bean:class [null];范围=; 抽象= FALSE; lazyInit = TRUE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE;初级= FALSE; factoryBeanName = elasticConfiguration; factoryMethodName = elasticsearchTemplate; initMethodName = NULL; destroyMethodName =(推断);在类路径资源中定义 [XXX / XXX / ElasticConfiguration.class]] 用[Root bean:class [null];范围=;抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE; 初级= FALSE; factoryBeanName = org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration; factoryMethodName = elasticsearchTemplate; initMethodName = NULL; destroyMethodName =(推断);在类路径资源中定义 [组织/ springframework的/引导/自动配置/数据/ elasticsearch / ElasticsearchDataAutoConfiguration.class]]
我还尝试在类
上添加以下注释@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
与以下属性相关联,但效果不佳
org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.xxx.ElasticConfiguration
我该怎么办?
答案 0 :(得分:1)
好的,我找到的唯一解决方案是使用以下属性
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration
请注意,使用folling注释对我不起作用...
@EnableAutoConfiguration(exclude={ElasticsearchDataAutoConfiguration.class})
修改强>
实际上我的问题是我的方法elasticsearchTemplate()
正在返回ElasticsearchOperations
而不是ElasticsearchTemplate
。
更改签名时,@ Lazy注释似乎有效。