Springboot读取配置文件中的属性

本案例通过springboot整合邮件给大家演示一下读取配置文件

对于springboot整合邮件感兴趣的参考:springboot整合邮件

 在账户的下面有一个开启SMTP协议的开关并进行密码验证:

以下就是你的授权码:

引入pom包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

配置邮箱基本信息

spring:
  mail:
    # 配置 SMTP 服务器地址
    host: smtp.qq.com
    # 发送者邮箱
    username: 790933839@qq.com
    # 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
    password: vjstfghblprwbdbd
    # 端口号465或587
    port: 587 
    # 默认的邮件编码为UTF-8
    default-encoding: UTF-8
    # 配置SSL 加密工厂
    properties:
      mail:
        smtp:
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
        #表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
        debug: true
myapp:
mail:
enable: true
default-subject: this is a maill
recevie-Person: 3405608341@qq.com
security-Person: 3405608341@qq.com

@ConfigurationProperties

package com.macro.cloud.properties;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author shkstart
 * @create 2021-07-04 17:08
 */
@Configuration
@ConfigurationProperties("myapp.mail")
public class MailModuleProperties {
    private Boolean enable=Boolean.TRUE;
    private String defaultSubject;
    private String receviePerson;
    private String securityPerson;

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public String getDefaultSubject() {
        return defaultSubject;
    }

    public void setDefaultSubject(String defaultSubject) {
        this.defaultSubject = defaultSubject;
    }

    public String getReceviePerson() {
        return receviePerson;
    }

    public void setReceviePerson(String receviePerson) {
        this.receviePerson = receviePerson;
    }

    public String getSecurityPerson() {
        return securityPerson;
    }

    public void setSecurityPerson(String securityPerson) {
        this.securityPerson = securityPerson;
    }
}

Environment

package com.macro.cloud.properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author shkstart
 * @create 2021-07-04 18:16
 */
@Component
public class MailSenderHandler {
    @Autowired
    private MailModuleProperties MailModuleProperties;
    @Autowired
    JavaMailSender javaMailSender;
    private  String maillSenderAccount;
    @Autowired
    private Environment env;


    public void sendSimpleMail() {
        if(MailModuleProperties.getEnable()){
            this.maillSenderAccount = env.getProperty("spring.mail.username");
            // 构建一个邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            // 设置邮件主题
            message.setSubject(MailModuleProperties.getDefaultSubject());
            // 设置邮件发送者,这个跟application.yml中设置的要一致
            message.setFrom(maillSenderAccount);
            // 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似
             message.setTo(MailModuleProperties.getReceviePerson());
            message.setTo();
            // 设置邮件抄送人,可以有多个抄送人
            message.setCc(MailModuleProperties.getSecurityPerson());
            // 设置隐秘抄送人,可以有多个
//            message.setBcc("7******9@qq.com");
            // 设置邮件发送日期
            message.setSentDate(new Date());
            // 设置邮件的正文
            message.setText("这是测试邮件的正文");
            // 发送邮件
            javaMailSender.send(message);
        }

} }

执行方法http://localhost:8001/user/aa

可以发现这三种方式都可以使用,但是我们如果需要读取spring默认的配置信息,需要使用Environment

原文地址:https://www.cnblogs.com/cj8357475/p/14969707.html