---
title: "spring boot启动文件 或 自定义 配置文件 值获取"
author: "ch_dj"
date: "2019-06-25T17:11:10Z"
tags: []
categories: ["编程语言"]
slug: "spring-boot"
draft: false
lastmod: "2019-06-25T17:11:10Z"
id: 2442147
url: "https://ch-dj.iteye.com/blog/2442147"
---

1.自定义配置文件获取

：

es.properties  在cn.zokoo.es.esclient  下

```
@Configuration

@ConfigurationProperties(prefix="elasticsearch")

@PropertySource("classpath:cn/zokoo/es/esclient/es.properties") //配置文件路径

@Data//注：一定要设置 get  set ,这里用lombok 

public class ElasticTestC  {

    private String hosts;

    @Bean 

    public Object createInstanc22e() {

        System.out.println(hosts);

        return "";

    }

}
```

 

 

 

2.启动文件值获取：如 application.properties  或 application.yml

如：kafka:

       cluster: my-kafka-clu

       port: 6379

```
@ComponentScan(basePackages = {"cn.zokoo"})

@SpringCloudApplication

@EnableAutoConfiguration//一定要配置

public class GoodsApplication {

    public static void main(String[] args) {

        SpringApplication.run(GoodsApplication.class, args);

    }

}
```

 

 

获取：

```
@Configuration

public class ElasticSearchConfiguration  {

    @Value("127.0.0.1:9200,127.0.0.1:8200,127.0.0.1:7200")

    private String host;

    

    @Value("${kafka.cluster}")

    private String kafkaClus;

}
```