【问题标题】:How to create a repository instance in main method of Spring Boot?如何在 Spring Boot 的 main 方法中创建存储库实例?
【发布时间】:2021-10-05 08:56:05
【问题描述】:

我需要设置 SSL 的属性以在我的 Spring Boot 应用程序的 main 方法中启用 HTTPS。我的代码如下所示:

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

}

它警告我在静态方法上不允许使用 @Autowired。执行时会给出空异常。 我读过这个 Can't use @Autowired JPA Repository in Main method of Spring Boot application

但它没有提供任何关于我如何做到这一点的信息。我不想使用属性文件。

【问题讨论】:

  • 你没有,因为在你目前的情况下,你会有鸡和蛋的问题。你应该做的是创建一个PropertySource,它使用 JDBC 从数据库中检索设置并将其作为环境的一部分,以便用于查找。

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

我不会在main 方法中做这样的事情。让 Spring 运行,然后添加您的配置。

我将创建一个 runner 类,一旦设置了 Spring 上下文,它将执行我需要的任何操作。

例子:

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

【讨论】:

  • 如何在 run() 中获取 SpringApplication 实例?
  • 在我看来,你做的整个事情都太复杂了。从我看到你试图通过你的数据库设置一些属性,你可以从你的数据库中获取你的属性值,然后更新/刷新它们。 baeldung.com/spring-reloading-properties
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 2018-02-15
相关资源
最近更新 更多