欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot启动文件 或 自定义 配置文件 值获取

程序员文章站 2022-07-12 22:11:09
...

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;

}