Spring Boot 自定义配置key @ConfigurationProperties

161阅读模式

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

配置文件application.properties

spring.application.name=springbootdemo文章源自懂站帝-http://www.sfdkj.com/17077.html

server.port=8080文章源自懂站帝-http://www.sfdkj.com/17077.html

mail.username=application-duan文章源自懂站帝-http://www.sfdkj.com/17077.html

mail.password=application-duan123456文章源自懂站帝-http://www.sfdkj.com/17077.html

测试代码类文章源自懂站帝-http://www.sfdkj.com/17077.html

import org.springframework.beans.factory.annotation.Autowired;文章源自懂站帝-http://www.sfdkj.com/17077.html

import org.springframework.beans.factory.annotation.Value;文章源自懂站帝-http://www.sfdkj.com/17077.html

import org.springframework.context.annotation.PropertySource;文章源自懂站帝-http://www.sfdkj.com/17077.html

import org.springframework.web.bind.annotation.RequestMapping;文章源自懂站帝-http://www.sfdkj.com/17077.html

import org.springframework.web.bind.annotation.RestController;文章源自懂站帝-http://www.sfdkj.com/17077.html

@RestController文章源自懂站帝-http://www.sfdkj.com/17077.html

@RequestMapping("/task")文章源自懂站帝-http://www.sfdkj.com/17077.html

public class TaskController {文章源自懂站帝-http://www.sfdkj.com/17077.html

@Value("${mail.username}")文章源自懂站帝-http://www.sfdkj.com/17077.html

private String userName;文章源自懂站帝-http://www.sfdkj.com/17077.html

@Value("${mail.password}")文章源自懂站帝-http://www.sfdkj.com/17077.html

private String password;文章源自懂站帝-http://www.sfdkj.com/17077.html

@RequestMapping(value = { "/", "" })文章源自懂站帝-http://www.sfdkj.com/17077.html

public String hellTask() {文章源自懂站帝-http://www.sfdkj.com/17077.html

System.out.println("userName:" + userName);文章源自懂站帝-http://www.sfdkj.com/17077.html

System.out.println("password:" + password);

return "hello task !!";

}

}

结果:

userName:application-duan

password:application-duan123456

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

读取mail.properties配置

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 !!";

}

}

结果:

userName:false

password:me@localhost

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

@ConfigurationProperties(prefix ="db.clickhouse")

实例代码

配置类

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

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"

懂站帝
  • 本文由 发表于 2022年7月2日 23:45:01
  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至395045033@qq.com举报,一经查实,本站将立刻删除。
评论  0  访客  0