Skip to content

Add Environment.getProperty(String name, Class<T> targetType, T defaultValue); [SPR-8322] #12971

@spring-projects-issues

Description

@spring-projects-issues

Keith Donald opened SPR-8322 and commented

e.g. would turn this:

@Bean
public JavaMailSender mailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setDefaultEncoding("UTF-8");
    mailSender.setHost(environment.getProperty("mail.host"));
    Integer port = environment.getProperty("mail.port", Integer.class);
    if (port != null) {
        mailSender.setPort(port);
    }
    mailSender.setUsername(environment.getProperty("mail.username"));
    mailSender.setPassword(environment.getProperty("mail.password"));
    Properties properties = new Properties();
    Boolean mailSmtpAuth = environment.getProperty("mail.smtp.auth", Boolean.class);
    if (mailSmtpAuth != null) {
        properties.put("mail.smtp.auth", mailSmtpAuth);
    }
    Boolean mailSmtpStartTls = environment.getProperty("mail.smtp.starttls.enable", Boolean.class);
    if (mailSmtpAuth != null) {
        properties.put("mail.smtp.auth", mailSmtpStartTls);
    }
    mailSender.setJavaMailProperties(properties);
    return mailSender;
}

into this:

@Bean
public JavaMailSender mailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setDefaultEncoding("UTF-8");
    mailSender.setHost(environment.getProperty("mail.host"));
    mailSender.setPort(port, environment.getProperty("mail.port", Integer.class, 25));
    mailSender.setUsername(environment.getProperty("mail.username"));
    mailSender.setPassword(environment.getProperty("mail.password"));
    Properties properties = new Properties();
    properties.put("mail.smtp.auth", environment.getProperty("mail.smtp.auth", Boolean.class, false));
    properties.put("mail.smtp.auth", environment.getProperty("mail.smtp.starttls.enable", Boolean.class, false));
    mailSender.setJavaMailProperties(properties);
    return mailSender;
}

Attachments:

Referenced from: commits 3622c6f, dc2d5c1

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions