小编Sir*_*tes的帖子

Spring Boot-@Value注释不起作用

我尝试使用SmtpAuthenticator创建邮件服务。组件已正确启动,但用户名和密码字段中为空值。为什么?

@Component
public class SmtpAuthenticator extends Authenticator {

    private static final Logger LOG = 
    LogManager.getLogger(SmtpAuthenticator.class.getSimpleName());

    @Value("${spring.mail.username}")
    private String username;
    @Value("${spring.mail.password}")
    private String password;

    public SmtpAuthenticator() {
        LOG.info(SmtpAuthenticator.class.getSimpleName() + " started...");
        LOG.debug("username=" + username);
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            LOG.debug("Username and password are correct...");
            return new PasswordAuthentication(username, password);
        }
    LOG.error("Not correct mail login data!");
    return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

java spring annotations spring-boot

4
推荐指数
1
解决办法
4968
查看次数

标签 统计

annotations ×1

java ×1

spring ×1

spring-boot ×1