当前位置: 首页 > 知识库问答 >
问题:

如何在Spring boot的main方法中创建存储库实例?

伯君浩
2023-03-14
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在静态方法上是不允许的。当执行时,它给出null异常。我读过这个不能在Spring Boot应用程序的主方法中使用@Autowired JPA存储库

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

共有1个答案

微生俊捷
2023-03-14

我不会在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
    }
}
 类似资料:
  • 我正在尝试在从CrudRepository扩展的存储库接口上执行我试图在方法中使用存储库实现来执行此操作。当我运行以下代码时,线程在 UpdateTasksService List 中永久等待

  • 经过安装Spring屋顶的痛苦。我正在尝试创建一个项目。然而,在通过spring shell创建了pojo之后,我现在正在创建spring存储库。我运行了以下命令来创建POJO 到目前为止一切都很好,但麻烦来了: 我立刻得到这个消息——实体选项应该是一个实体。我被卡住了。我尝试删除pojo并运行以下命令: 我得到的消息是:< code >“选项‘test automatically’,‘activ

  • 我试图创建一个简单的网站,其中托管主题和评论。我已经从主题开始,并为它们创建了存储库: 我已经在servlet上下文中定义了存储库的路径。xml: 现在,我想在我的存储库中包含注释,但以下代码不起作用: 我的项目甚至都没建好。你能给我一个建议吗,如何为多个实体创建存储库(主题类和注释类是用@Entity声明的)? 我面对的是: TopicRepository类图标上有HDD图片 org.sprin

  • 如何在一个类中创建和实例化jpa存储库?我现在的情况是,我必须在一个泛型类中为不同的实体创建存储库。 我可以很容易地为Neo4j存储库这样做, 对于JpaRepostory,我检查了留档,发现了这个, 我不确定如何在上面的代码中实例化工厂。 另外,我不能像为Neo4j那样通过指定域类来创建存储库吗?

  • 我想在GitHub资源库中创建一个文件夹,并想在该文件夹中添加文件。我如何实现这一点?

  • 我正在做一个项目,我们有很多实体,我们将在这些实体上进行CRUD操作。我创建了一个基本实体类,在所有其他实体中,我扩展了基本实体类,该类具有诸如created\u date、created\u by、last\u updated\u date、last\u updated\u by等公共字段。现在,我想在Spring CrudRepository方法上实现aspect,并在保存时设置上述字段。 我