前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 自定义配置key @ConfigurationProperties(prefix = "db.clickhouse")

Spring Boot 自定义配置key @ConfigurationProperties(prefix = "db.clickhouse")

作者头像
一个会写诗的程序员
发布2021-12-16 11:31:57
4560
发布2021-12-16 11:31:57
举报

方案1:使用@Value读取application.properties里的配置内容

配置文件application.properties

代码语言:javascript
复制
 spring.application.name=springbootdemo
 server.port=8080
 mail.username=application-duan
 mail.password=application-duan123456
 

测试代码类

代码语言:javascript
复制
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.PropertySource;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 @RestController
 @RequestMapping("/task")
 public class TaskController {
     @Value("${mail.username}")
     private String userName;
 

     @Value("${mail.password}")
     private String password;
     @RequestMapping(value = { "/", "" })
     public String hellTask() {
         System.out.println("userName:" + userName);
         System.out.println("password:" + password);
         return "hello task !!";
     }
 }
 

结果:

代码语言:javascript
复制
 userName:application-duan
 password:application-duan123456
 

方案2:使用@Value+@PropertySource读取其它配置文件(多个)内容

读取mail.properties配置

代码语言:javascript
复制
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.PropertySource;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 @RestController
 @RequestMapping("/task")
 @PropertySource("classpath:mail.properties")
 public class TaskController {
     @Value("${mail.smtp.auth}")
     private String userName;
 

     @Value("${mail.from}")
     private String password;
     @RequestMapping(value = { "/", "" })
     public String hellTask() {
         System.out.println("userName:" + userName);
         System.out.println("password:" + password);
         return "hello task !!";
     }
 }
 

结果:

代码语言:javascript
复制
 userName:false
 password:me@localhost
 

方案3.对象映射的方式:@ConfigurationProperties

代码语言:javascript
复制
@ConfigurationProperties(prefix ="db.clickhouse")

实例代码

配置类

代码语言:javascript
复制
 import lombok.Data;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
 /**
 * ClickHouse圈选数据库的常量
 */
 @Data
 @Configuration
 @ConfigurationProperties(prefix ="db.clickhouse")
 public class DbClickHouseConfig {
 String db_name ="ecom_dmp_ch";
 String db_cluster_name ="ecom_public";
 String user_tag_select ="user_tag_select";
 String user_behavior_select ="user_behavior_select";
 String user_tag_shard_column ="uid";
 String item_select_table ="item_tag_select";
 String item_shard_column ="item_id";
 String seller_select_table ="seller_tag_select";
 String seller_shard_column ="seller_id";
 String partition ="p_date";
 String partition_date_format ="yyyy-MM-dd";
 String driver_id ="clickhouse_01";
 }
 

application.yml

代码语言:javascript
复制
 db:
 clickhouse:
 db_name:"ecom_dmp_ch_select"
     db_cluster_name:"ecom_public"
     user_tag_select:"user_tag_select"
     user_behavior_select:"user_behavior_select"
     user_tag_shard_column:"uid"
     item_select_table:"item_tag_select"
     item_shard_column:"item_id"
     seller_select_table:"ecom_dmp_ch"
     seller_shard_column:"seller_id"
     partition:"p_date"
     partition_date_format:"yyyy-MM-dd"
     driver_id:"clickhouse_01"
 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021/8/13 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 方案1:使用@Value读取application.properties里的配置内容
  • 方案2:使用@Value+@PropertySource读取其它配置文件(多个)内容
  • 方案3.对象映射的方式:@ConfigurationProperties
    • 实例代码
      • 配置类
        • application.yml
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档