Home > Back-end >  How to create a repository instance in main method of Spring Boot?
How to create a repository instance in main method of Spring Boot?

Time:10-05

I need to set properties for SSL to enable HTTPS in my Spring Boot application's main method. My code looks like this:

import com.our.Task.configuration.FileStorageProperties;
import com.our.Task.entities.ApplicationHttpsSettingsEntity;
import com.our.Task.repository.ApplicationHttpsSettingsEntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Properties;

@SpringBootApplication
@EnableConfigurationProperties({FileStorageProperties.class})
public class OurTaskApplication
{
    @Autowired
    static ApplicationHttpsSettingsEntityRepository applicationHttpsSettingsEntityRepository;

    public static void main(String[] args)
    {
        
        ApplicationHttpsSettingsEntity applicationsHttpsSettingsEntity = applicationHttpsSettingsEntityRepository.getById(44);

        SpringApplication application = new SpringApplication(OurTaskApplication.class);

        Properties properties = new Properties();

        if (applicationsHttpsSettingsEntity.getUseHttps() > 0)
        {
            properties.put("server.ssl.key-store", applicationsHttpsSettingsEntity.getKeyStore());
            properties.put("server.ssl.key-store-password", applicationsHttpsSettingsEntity.getKeyStorePassword());
            properties.put("server.ssl.key-store-type", applicationsHttpsSettingsEntity.getKeyStoreType());
            properties.put("server.ssl.key-alias", applicationsHttpsSettingsEntity.getKeyAlias());
        }

//        SpringApplication.run(OurTaskApplication.class, args);

        application.setDefaultProperties(properties);
        ConfigurableApplicationContext ctx = application.run(args);
    }

}

It gives me warning that @Autowired is not allowed on static methods. When executed it gives null exception. I have read this Can't use @Autowired JPA Repository in Main method of Spring Boot application

But it doesn't give any info on how I can do this. I don't want to use properties file.

CodePudding user response:

I wouldn't do something like this in the main method. Let Spring run and then add your configurations.

I would create a runner class that will do whatever I need once the Spring context is set up.

Example:

@Component
@AllArgsConstructor
public class StartRunner implements ApplicationRunner {

    /* Add whatever Bean you need here and autowire them through the constructor or with @Autowired */


    @Override
    public void run(ApplicationArguments args) throws Exception {
        // Do whatever you need here inside
    }
}
  • Related