【发布时间】:2015-12-26 13:14:54
【问题描述】:
我有一个工作的 Spring Boot Elasticsearch 应用程序,它使用两个配置文件之一:application.dev.properties 或 application.prod.properties。那部分工作正常。我在让外部弹性搜索从 application.xxx.properties 中读取时遇到问题。
这行得通:
@Configuration
@PropertySource(value = "classpath:config/elasticsearch.properties")
public class ElasticsearchConfiguration {
@Resource
private Environment environment;
@Bean
public Client client() {
TransportClient client = new TransportClient();
TransportAddress address = new InetSocketTransportAddress(
environment.getProperty("elasticsearch.host"),
Integer.parseInt(environment.getProperty("elasticsearch.port"))
);
client.addTransportAddress(address);
return client;
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
}
但显然不能解决我的多环境问题。
我还尝试了主机和端口变量的 @Value 注释,但没有成功。
如何转换上述内容以从应用程序属性文件中读取其值,或根据我要运行的配置文件选择不同的 @PropertySource 文件?
spring.data.elasticsearch.properties.host = 10.10.1.10
spring.data.elasticsearch.properties.port = 9300
谢谢
【问题讨论】:
-
你为什么不只是使用 Spring Boot 而是试图解决它。 Spring boot 已经根据您选择的配置文件加载了一个属性文件。所以你基本上让它变得很难......而且Spring Boot已经为你配置了ElasticSearch,所以你为什么要自己再做一次......
标签: java spring-boot elasticsearch