spring boot 读取配置文件

来源:互联网 发布:迪克斯特拉算法 编辑:程序博客网 时间:2024/05/17 11:33

spring boot 读取配置文件有两种方式:

一种是通过@PropertySource注解,然后使用@Value逐个注入配置

package com.cn.core.mail;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;/** * 这种方式可以读取配置文件 也可以通过启动脚本 --spring.config.location 引用外部文件进行覆盖 * @author jianghd * */@Configuration@PropertySource("classpath:mail.properties")public class ConfigLoader2 {@Value("${mail.server}")private  String server;// 发件人邮箱地址@Value("${mail.sender}")private  String sender;// 发件人邮箱用户名@Value("${mail.username}")private  String username;// 发件人邮箱密码@Value("${mail.password}")private  String password;// 发件人显示昵称@Value("${mail.nickname}")private  String nickname;//收件人@Value("${mail.receiver}")private  String receiver;public String getServer() {return server;}public void setServer(String server) {this.server = server;}public String getSender() {return sender;}public void setSender(String sender) {this.sender = sender;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getReceiver() {return receiver;}public void setReceiver(String receiver) {this.receiver = receiver;}}

另外一种方式是通过@ConfigurationProperties注解,通过getter、setter方法注入及获取配置。

package com.cn.core.mail;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Repository;/** * 这种方式只会读取classpath下的配置文件, * @Configuration 配置 *  * @Component 表示带注释的类是“组件”,此类被认为是自动检测,使用基于注释的配置和路径扫描 *      其他类级别注释可被视为标识。也可认为是一个组件,通常是一种特殊的组件: *  * @author jianghd * */@Configuration@ConfigurationProperties(locations="classpath:mail.properties",prefix = "mail")public class ConfigLoader {// 邮件发送SMTP主机private  String server;// 发件人邮箱地址private  String sender;// 发件人邮箱用户名private  String username;// 发件人邮箱密码private  String password;// 发件人显示昵称private  String nickname;//收件人private  String receiver;public  String getServer() {return server;}public void setServer(String server) {this.server = server;}public void setSender(String sender) {this.sender = sender;}public void setUsername(String username) {this.username = username;}public void setPassword(String password) {this.password = password;}public void setNickname(String nickname) {this.nickname = nickname;}public void setReceiver(String receiver) {this.receiver = receiver;}public  String getSender() {return sender;}public  String getUsername() {return username;}public  String getPassword() {return password;}public  String getNickname() {return nickname;}public  String getReceiver() {return receiver;}}

配置文件 mail.properties

#mail sender settings# for example: smtp.sina.cnmail.server=smtp.126.com#the sender mailmail.sender=xxxx@126.commail.nickname=qylhjhd#sender mail usernamemail.username=xxxx@126.com#sender mail passwordmail.password=mail.receiver=xxxx@qq.com

注意:有个小坑

如果定义一个键值对 user.name=xxx ,或者user=xxx,这里会读取不到对应写的属性值。为什么呢?Spring Boot 的默认 StandardEnvironment 首先将会加载 “systemEnvironment" 作为首个PropertySource. 而 source 即为System.getProperties().当 getProperty时,按照读取顺序,返回 “systemEnvironment" 的值.即 System.getProperty("user.name"),linux 系统机子会读当前的用户名。所以设置配置文件名称的时候需要注意,尽量避免与系统环境变量名称重复

另外spring boot 获取配置属性的优先级:
1.命令行参数
2.java:comp/env 里的 JNDI 属性
3.JVM 系统属性
4.操作系统环境变量
5.RandomValuePropertySource 属性类生成的 random.* 属性
6.应用以外的 application.properties(或 yml)文件
7.打包在应用内的 application.properties(或 yml)文件
8.在应用 @Configuration 配置类中,用 @PropertySource 注解声明的属性文件
9.SpringApplication.setDefaultProperties 声明的默认属性