如何在启动应用程序时延迟加载存储库? [英] How to make repository be loaded lazily when I start application?

查看:21
本文介绍了如何在启动应用程序时延迟加载存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发过程中,随着班级数量的增加,应用启动需要2分钟多的时间.我认为这会降低我们的开发效率...

During development, after number of class increased, it take over 2 minutes until application is started.I think it makes our develop efficiency low...

并且我发现如果我们添加'lazyInit=true',如果我添加这个选项,大多数类将被延迟加载,但即使我添加这个选项,也会加载存储库.

And I found that if we add 'lazyInit=true', If I add this option, most of classes will be loaded lazily, but repositories are loaded even if I add this option.

@ComponentScan(basePackageClasses = LazyApplication.class,lazyInit=true)
@EnableAutoConfiguration(
)
public class LazyApplication {

    public static void main(String[] args) {
        SpringApplication.run(LazyApplication.class, args);
    }
}

实际上,我们的系统有超过 300 个仓库和实体,所以如果可能的话,我也想让仓库变得懒惰.

Actually, our system have more than 300 repository and entity, so I want to make repository lazy too if possible.

我怎样才能让我的存储库启动应用程序时不加载而是第一次访问存储库时加载?

How can I make my repository will Not be loaded when I start application but loaded when I access to the repository for the first time?

推荐答案

您可以使用 @Order 注释在您的配置类上以定义加载顺序.最高优先级建议首先运行.数字越小,优先级越高.例如

You can use @Order annotation on your configuration classes to define load ordering.The highest precedence advice runs first. The lower the number, the higher the precedence. e.g

@Component
@Order(2)
public class MyRepo {
    public String getName() {
        return "some value";
    }
}

这篇关于如何在启动应用程序时延迟加载存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆