Spring boot Build issue

Tags: , ,



**Custom configuration for mail sender **

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

here I am trying to fetch the password from the database but the problem is while building the app it calls the repository which leads to failure as IP whitelisting issue it is getting error: unable to acquire JDBC connection.

How to stop these repo calls while building the app

Answer

You can use @Lazy annotation at the Configuration level. So that the beans will be created at runtime when requested for First-time.

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

Reference: https://www.baeldung.com/spring-lazy-annotation



Source: stackoverflow