Spring boot custom configuration key @ configurationproperties (prefix = 'dB. Clickhouse')

A programmer who can write poetry 2022-01-26 11:14:19 阅读数:583

spring boot custom configuration key

programme 1: Use @Value Read application.properties Configuration content in

The configuration file application.properties

 spring.application.name=springbootdemo
server.port=8080
mail.username=application-duan
mail.password=application-duan123456

Test code class

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

result :

 userName:application-duan
password:application-duan123456

programme 2: Use @[email protected] Read other configuration files ( Multiple ) Content

Read mail.properties To configure

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

result :

 userName:false
password:[email protected]

programme 3. How objects are mapped :@ConfigurationProperties

@ConfigurationProperties(prefix ="db.clickhouse")

The sample code

Configuration class

 import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* ClickHouse Circle the constants of the database
*/
@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"
copyright:author[A programmer who can write poetry],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261114174739.html