1

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.

Ram
  • 1,225
  • 2
  • 24
  • 48
  • 2
    You don't, as in your current situation you would have a chicken and egg problem. What you should do is make a `PropertySource` that uses JDBC to retrieve the settings from the database and make that part of the environment so it will be used to do a lookup. – M. Deinum Oct 05 '21 at 09:01

1 Answers1

2

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
    }
}
Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • How do I get SpringApplication instance in run() ? – Ram Oct 05 '21 at 09:53
  • In my opinion you are doing the whole thing too complicated. From what I see you are trying to set some properties through your DB, you could just get the values of your properties from your DB and then update/refresh them. https://www.baeldung.com/spring-reloading-properties – Renis1235 Oct 05 '21 at 09:59