被这个问题困扰了好几天....
在spring中, 从资源文件向bean中注入值非常简单, 只需要properties文件被spring加载, 然后在被spring管理的类写响应的属性, 然后 @Value("${SERVER_URL") 的方式就可以取到值了
在springboot中, 同样的方式也可以取到值, 但未免感觉有点low, 所以苦苦寻觅好几天,
在这儿以创建一个ESClient的方式进行说明:
1, 在App.java中加入注解
@EnableConfigurationProperties
我的类上有注解, @EnableAutoConfiguration, 所以没有加
2, 将 elasticsearch.properties 文件放置在 source/ES/下
3, 然后在需要注入的类上加入注解:
@Component
@ConfigurationProperties(prefix= "escluster.transport")
@PropertySource("classpath:ES/elasticsearch.properties")
因为 5.1.0 以后, 取消了ConfigurationProperties中location属性, 所以使用 PropertySource 进行了替代, 也正是这个注解需要步骤一种的注解
查了挺多博客才找到原因: http://www.jianshu.com/p/b71845c142d0, 为此还找了个vpn, 挺好用, 想要可以联系 [email protected]
4, 完整的ESClient类代码
package com.iwhere.easy.travel.tool;
import java.net.InetSocketAddress;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/**
* 获取esclient工具类
*
* @author wenbronk
* @time 2017年4月5日 上午11:29:52 2017*/@Component
@ConfigurationProperties(prefix= "escluster.transport")
@PropertySource("classpath:ES/elasticsearch.properties")public classESClient {private Logger LOGGER = LoggerFactory.getLogger(ESClient.class);privateString name;privateString ip;private intport;privateboolean sniff;privateboolean ignore_cluster_name;private intping_timeout;private intnodes_sampler_interval;/**
* @return*/@Bean(name= "client")publicClient getEsClient() {
Client client= null;
Settings settings= Settings.builder().put("cluster.name", name)
.put("client.transport.sniff", sniff)//.put("client.transport.ignore_cluster_name",//ESCLUSTER_IGNORE_NAME)//.put("client.transport.ping_timeout", ESCLUSTER_TIMEOUT)//.put("client.transport.nodes_sampler_interval",//ESCLUSTER_INTERVAL)
.build();
client= newPreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(newInetSocketAddress(ip, port)));
LOGGER.info("transport client has created");returnclient;
}publicLogger getLOGGER() {returnLOGGER;
}public voidsetLOGGER(Logger lOGGER) {
LOGGER=lOGGER;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getIp() {returnip;
}public voidsetIp(String ip) {this.ip =ip;
}public intgetPort() {returnport;
}public void setPort(intport) {this.port =port;
}publicboolean isSniff() {returnsniff;
}public voidsetSniff(boolean sniff) {this.sniff =sniff;
}publicboolean isIgnore_cluster_name() {returnignore_cluster_name;
}public voidsetIgnore_cluster_name(boolean ignore_cluster_name) {this.ignore_cluster_name =ignore_cluster_name;
}public intgetPing_timeout() {returnping_timeout;
}public void setPing_timeout(intping_timeout) {this.ping_timeout =ping_timeout;
}public intgetNodes_sampler_interval() {returnnodes_sampler_interval;
}public void setNodes_sampler_interval(intnodes_sampler_interval) {this.nodes_sampler_interval =nodes_sampler_interval;
}
}
现在就完成了springboot从properties文件向bean中注值问题的解决
原文:http://www.cnblogs.com/wenbronk/p/6668506.html