具有@Configuration的Spring @RefreshScope不会动态刷新

问题描述 投票:0回答:1

我正在使用spring boot版本(2.2.5.RELEASE)和spring-cloud-dependencies(Hoxton.SR3)。

我有一个如下所示的课程:

    @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;
    }

}

我正在使用Spring cloud配置从git获取我的信息。在同一项目中,我有一个下面的类:

@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);
            }

        }

[当我用“ actuator / refresh”刷新URL时,restcontroller得到成功刷新,但没有刷新@Configuration类,如前所述。

更新:下面的类我正在使用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) {
....
}

那么是否可以将刷新作用域注释与配置注释一起使用?

谢谢您的任何建议

我正在使用spring boot版本(2.2.5.RELEASE)和spring-cloud-dependencies(Hoxton.SR3)。我有一个如下所示的类:@RefreshScope @Configuration公共类JavaMailConfig {@ ...

spring-boot spring-mvc spring-cloud-config
1个回答
0
投票

以防万一有人遇到这个问题,我设法使它起作用,但是我不知道它是否正确。

© www.soinside.com 2019 - 2024. All rights reserved.