0
votes

I am using spring boot version(2.2.5.RELEASE) and spring-cloud-dependencies(Hoxton.SR3).

I have a class as shown below:

    @RefreshScope
    @Configuration
    public class JavaMailConfig {


    @Value("${email.common.config.host:ERROR: Could not load email config host}")
    private String host;

    @Value("${email.common.config.port:ERROR: Could not load email config port}")
    private String port;

    @Value("${email.common.config.transport.protocol:ERROR: Could not load email config protocol}")
    private String protocol;

    @Value("${email.common.config.username:ERROR: Could not load email config username}")
    private String mailUserName;

    @Value("${email.common.config.password:ERROR: Could not load email config passsword}")
    private String mailPassword;

    @Value("${email.common.config.password:ERROR: Could not load email config smtpAuth}")
    private String smtpAuth;

    @Value("${email.common.config.password:ERROR: Could not load email config startTlsEnable}")
    private String startTlsEnable;

    @Value("${email.common.config.password:ERROR: Could not load email config sslTrust}")
    private String sslTrust;

    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        CommonUtility.setPort(mailSender, port);

        mailSender.setUsername(mailUserName);
        mailSender.setPassword(mailPassword);

        Properties props = mailSender.getJavaMailProperties();

        props.put("mail.transport.protocol", protocol);

        props.put("mail.smtp.auth", smtpAuth);
        props.put("mail.smtp.starttls.enable", startTlsEnable);
        props.put("mail.smtp.ssl.trust", sslTrust);

        return mailSender;
    }

}

I am using spring cloud config to get my information from git. In the same project I have a class below:

@RestController
@RefreshScope
@RequestMapping("/email")

public class EmailController {



    private static final Logger LOG = LoggerFactory.getLogger(EmailController.class);

        @Autowired
        SendMailService sendMailService;



        @Value("${email.common.config.username:ERROR: Could not load email config username}")
        private String mailUserName;



        @PostMapping(value = "/sendMail")
        //Note:Not to remove @RequestBody and @RequestBody as swagger UI will not interpret it correctly
        public ResponseEntity<String> sendMail(@RequestBody  EmailRequestDto emailRequestDto) {

            if (checkAllEmailAddValid(emailRequestDto)) {

                System.out.println("mailUserName from controller " + mailUserName);
                System.out.println("profile " + profile);
                sendMailService.sendEmail(emailRequestDto);

                LOG.debug("Send mail completed successfully ");
                return new ResponseEntity<>("Mail has been sent successfully", HttpStatus.OK);
            } else {
                LOG.error("Email addresse provided is  invalid");
                return new ResponseEntity<>("Email address provided is  invalid", HttpStatus.BAD_REQUEST);
            }

        }

When I refresh the url with "actuator/refresh" the restcontroller get refreshed successfully but not the @Configuration class as stated earlier.

Update: The class below I am using the JavaMailSender:

@Component
@RefreshScope
public class SendMailServiceImpl implements SendMailService {

    private static final Logger LOG = LoggerFactory.getLogger(SendMailServiceImpl.class);

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendEmail(EmailRequestDto emailRequestDto) {
....
}

So is it possible to use refresh scope annotation with Configuration annotation?

Thanks in advance for any advice

1
Where are you using your JavaMailConfig class? - S_learner
I have updated my question - basically i have a controller that is calling SendMailServiceImpl which autowire JavaMailSender - user1999453
SendServiceImpl itself is not within RefreshScope, so whenever you will be refreshing the context, its not going to re-initialize SendServiceImpl bean, which will still point to existing JavaMailConfig. Put @RefreshScope on your service class, and try refreshing again. Explanation can be found under, cloud.spring.io/spring-cloud-static/… - S_learner
I have updated the question with refreshScsope on SendServiceImpl class but it is still not refreshing - user1999453
see if '@ConfigurationProperties' can be utilized instead of using @Value. '@ConfiugrationProperties' should consider the refresh, devglan.com/spring-cloud/refresh-property-config-runtime - S_learner

1 Answers

0
votes

Just in case if someone have this issue I manage to make it work but I don't know if it the correct way though.

I have added the ConfigurationProperties annotation with the prefix which contains the key of my properties file on top of my bean annotation:

 @ConfigurationProperties(prefix = "email.common.config")
  @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        CommonUtility.setPort(mailSender, port);

        mailSender.setUsername(mailUserName);
        mailSender.setPassword(mailPassword);

        Properties props = mailSender.getJavaMailProperties();

        props.put("mail.transport.protocol", protocol);

        props.put("mail.smtp.auth", smtpAuth);
        props.put("mail.smtp.starttls.enable", startTlsEnable);
        props.put("mail.smtp.ssl.trust", sslTrust);

        return mailSender;
    }