我创建了以下配置(我复制了@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'vlasticsearchTemplate'的bean定义:替换[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 =(推断);在类路径资源中定义[org / springframework / boot / autoconfigure / data / elasticsearch / ElasticsearchDataAutoConfiguration.class]]
我还尝试在类上添加以下注释
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
与以下属性相关联,但它没有更好地工作
org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.xxx.ElasticConfiguration
我该怎么办 ?
1 回答
好的,我找到的唯一解决方案是使用以下属性
请注意,使用folling注释对我不起作用...
EDIT
实际上我的问题是我的方法
elasticsearchTemplate()
正在返回ElasticsearchOperations
而不是ElasticsearchTemplate
.更改签名时,@ Lazy注释似乎有效 .