久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

SpringBoot 使用 @Value 注解讀取配置文件給靜態變量賦值

瀏覽:79日期:2023-04-09 18:03:16

1、application.properties 配置文件

mail.username=xue@163.commail.password=xuemail.host=smtp.163.commail.smtp.auth=true

2、給普通變量賦值,直接在變量上添加 @Value 注解

import org.springframework.beans.factory.annotation.Value;public class MailConfig { @Value('${mail.username}') private String username; @Value('${mail.password}') private String password; @Value('${mail.host}') private String host;}

3、給靜態變量賦值,直接在靜態變量上添加 @Value 注解無效

SpringBoot 使用 @Value 注解讀取配置文件給靜態變量賦值

4、給靜態變量賦值

1、使用 set 方法

import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class MailConfig { public static String username; public static String password; public static String host; @Value('${mail.username}') public void setUsername(String username) { this.username = username; } @Value('${mail.password}') public void setPassword(String password) { this.password = password; } @Value('${mail.host}') public void setHost(String host) { this.host = host; }}

2、使用 @PostConstruct(推薦使用)

import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Componentpublic class MailConfig { public static String USERNAME; public static String PASSWORD; public static String HOST; @Value('${mail.username}') private String username; @Value('${mail.password}') private String password; @Value('${mail.host}') private String host; @PostConstruct public void init() { USERNAME = username; PASSWORD = password; HOST = host; }}

到此這篇關于SpringBoot 使用 @Value 注解讀取配置文件給靜態變量賦值的文章就介紹到這了,更多相關SpringBoot @Value 靜態變量賦值內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 伊川县| 万盛区| 石景山区| 汝南县| 扶风县| 丰宁| 潢川县| 宁都县| 东港市| 吴川市| 济南市| 开平市| 墨竹工卡县| 蓬莱市| 惠水县| 成武县| 湄潭县| 桃园县| 容城县| 普定县| 瓦房店市| 乐昌市| 六枝特区| 辉南县| 利川市| 上虞市| 塔河县| 五华县| 铜川市| 陵水| 兴义市| 渝中区| 临清市| 普宁市| 中超| 临安市| 奉贤区| 福泉市| 堆龙德庆县| 苗栗县| 托里县|