2017-07-19 285 views
0

我想在某些application.properties文件中设置弹性搜索自定义连接参数。 像弹簧引导自定义配置

myelastic.server = 192.168.1.1 
myelastic.port = 11111 

我需要加载应用程序启动这个值并创建传输客户端组件的弹性搜索5.4

我怎样才能在启动时加载此值模型/属性。 ?

谢谢。

回答

1

请你的模型类使用下面的代码:

@Value("${myelastic.server}") 
String server; 

@Value("${myelastic.port}") 
int port; 
1

您可以从春天访问或者使用@Value注释或自动装配环境属性。

使用@Value获取的属性值:

@Value("${myelastic.server}") 
private String elasticServer; 

@Value("${myelastic.port}") 
private int elasticPort; 

@Value("${elasticsearch.clustername}") 
private String EsClusterName; 

和下面创建传输客户端:

@Bean 
public Client client() throws Exception { 

    Settings esSettings = Settings.settingsBuilder() 
      .put("cluster.name", EsClusterName) 
      .build(); 

    return TransportClient.builder() 
      .settings(esSettings) 
      .build() 
      .addTransportAddress(
       new InetSocketTransportAddress(InetAddress.getByName(elasticServer), elasticPort)); 
}