spring启动构建问题

kdfy810k  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(247)

邮件发件人的自定义配置

@Configuration
    public class EmailConfig {

        private EmailProperties emailProp;

        @Autowired
        ConstantRepository constantRepository;

        public EmailConfig(EmailProperties emailProp) {
            this.emailProp = emailProp;
        }
        @Bean
        public JavaMailSender getJavaMailSender() {
Constants cons = constantRepository.findByConstantKeyAndStatus("DEFAULT_MAIL_ACCOUNT_CREDENTIAL",true);
        String password = cons.getValue();
}

在这里,我试图从数据库中获取密码,但问题是在构建应用程序时,它调用了存储库,这导致了失败,因为ip白名单问题它得到了错误:无法获取jdbc连接。
如何在构建应用程序时停止这些回购调用

7eumitmz

7eumitmz1#

您可以在配置级别使用@lazy注解。以便在首次请求时在运行时创建bean。

@Configuration
    @Lazy
    public class EmailConfig {

        private EmailProperties emailProp;

        @Autowired
        ConstantRepository constantRepository;

        public EmailConfig(EmailProperties emailProp) {
            this.emailProp = emailProp;
        }
        @Bean
        public JavaMailSender getJavaMailSender() {
Constants cons = constantRepository.findByConstantKeyAndStatus("DEFAULT_MAIL_ACCOUNT_CREDENTIAL",true);
        String password = cons.getValue();
}

参考文献:https://www.baeldung.com/spring-lazy-annotation

相关问题