被這個問題困擾了好幾天....
在spring中, 從資源文件向bean中注入值非常簡單, 只需要properties文件被spring加載, 然后在被spring管理的類寫響應的屬性, 然后 @Value("${SERVER_URL") 的方式就可以取到值了
在springboot中, 同樣的方式也可以取到值, 但未免感覺有點low, 所以苦苦尋覓好幾天,
在這兒以創建一個ESClient的方式進行說明:
0, 在pom.xml中導入依賴
為什么叫0呢, 因為你不導入, 加上(1) 的注解, 會有警告黃線提示你導入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
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, 挺好用, 想要可以聯系 wenbronk@163.com
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 class ESClient { private Logger LOGGER = LoggerFactory.getLogger(ESClient.class); private String name; private String ip; private int port; private boolean sniff; private boolean ignore_cluster_name; private int ping_timeout; private int nodes_sampler_interval; /** * @return */ @Bean(name = "client") public Client 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 = new PreBuiltTransportClient(settings).addTransportAddress( new InetSocketTransportAddress(new InetSocketAddress(ip, port))); LOGGER.info("transport client has created "); return client; } public Logger getLOGGER() { return LOGGER; } public void setLOGGER(Logger lOGGER) { LOGGER = lOGGER; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public boolean isSniff() { return sniff; } public void setSniff(boolean sniff) { this.sniff = sniff; } public boolean isIgnore_cluster_name() { return ignore_cluster_name; } public void setIgnore_cluster_name(boolean ignore_cluster_name) { this.ignore_cluster_name = ignore_cluster_name; } public int getPing_timeout() { return ping_timeout; } public void setPing_timeout(int ping_timeout) { this.ping_timeout = ping_timeout; } public int getNodes_sampler_interval() { return nodes_sampler_interval; } public void setNodes_sampler_interval(int nodes_sampler_interval) { this.nodes_sampler_interval = nodes_sampler_interval; } }
現在就完成了springboot從properties文件向bean中注值問題的解決
寫在最后, 不要在構造方法中使用 @Autowired的值, 猜測應該是對象創建完成后才會對值進行導入, 再次坑了很久
對於yml格式的文件, 注入值的方式為:
@Component @ConfigurationProperties(prefix = "interface.server.url") // @PropertySource("classpath:server_url.yml") public class BaseInterfaceUtil { private static Logger LOGGER = LoggerFactory.getLogger(BaseInterfaceUtil.class); private String geosot; @setGet }
yml文件的書寫格式:

如果是單獨的properties文件, 也可以使用
ResourceBundle.getBundle("fileName")
他會默認加載 classpath: 下的 fileName.properties文件, 並存入一個map集合中
5, 如果直接在application.yml中寫配置, 可以
package rdp.hive.phone.job.conf; import com.google.common.collect.Lists; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Arrays; /** * */ @Component @ConfigurationProperties(prefix = "es.connect") public class EsClientConfig { private String hosts; private Integer port; @Bean @ConditionalOnMissingBean(RestHighLevelClient.class) public RestHighLevelClient getRestHighLevelClient() { ArrayList<HttpHost> hostsArray = Lists.newArrayList(); Arrays.stream(hosts.split(", *")).forEach(host -> { hostsArray.add(new HttpHost(host, port, "http")); }); return new RestHighLevelClient(RestClient.builder(hostsArray.toArray(new HttpHost[0]))); } public String getHosts() { return hosts; } public void setHosts(String hosts) { this.hosts = hosts; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } }
然后在yml中配置:

系列原創, 轉載請注明出處, 謝謝 @wenbronk
