Spring boot 读取配置项 properties文件

需求

项目有测试环境与正式环境,我们做了一个发邮件的功能;邮件内容分别显示的内容格式不同

实现
1.配置properties文件(我这里直接写在application.properties)

#邮件配置
#邮件发送地址
mail.hicky=http://********:9020/PostMail/sendEmail
mail.seturl=http://test.***.com/pluto/p/TaskDetail/

#mail.hicky=http://*******:9020/PostMail/sendEmail
#mail.seturl=http://home.***.com/pluto/p/TaskDetail/

2.准备配置类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("mail")
//可以指定配置文件 如果不指定默认 application.properties
//@PropertySource("xxx.properties")
@Data
public class MailProperties {
    private String hicky;
    private String seturl;
}

3.使用测试

@Autowired
    private MailProperties mailProperties;

@PostMapping("Test")
    @ResponseBody
    @ApiOperation("添加测试")
    public HttpResultT add() throws IOException {
        HttpResultT httpResultT=new HttpResultT();
        return httpResultT.ok(mailProperties);
    }

效果
Spring boot 读取配置项 properties文件
后记
当然也有很多方法,比如配置文件监视,因为而异

上一篇:如何提取Apple的邮件数据库的邮箱地址


下一篇:Kafka学习之(七)搭建kafka可视化服务Kafka Eagle