【问题标题】:Dropwizard - Run init method in class on server startDropwizard - 在服务器启动时在类中运行 init 方法
【发布时间】:2015-10-15 22:17:34
【问题描述】:

我正在尝试将一堆数据从数据库中提取到应用程序的内存中。

我想在服务器启动时执行此操作,这样它就不会在运行时减慢应用程序的速度。

这在 Dropwizard 中是否可行(使用 Guice 依赖注入)?

【问题讨论】:

    标签: java guice dropwizard


    【解决方案1】:

    在应用程序启动时加载数据的简单方法。在 dropwizard 中,您可以在应用程序类的 run 方法中执行此操作。点击here参考。

    【讨论】:

      【解决方案2】:

      我在我的应用程序中使用了类似的方法,使用 UnitOfWorkAwareProxyFactory 在 My Application.Java 里面的 run 方法中,我创建了代理 DAO。

      public void run(Configuration configuration, Environment environment) throws Exception {
          // run method 
           .
           .
           .
      
          // At end of the method
          final MyDAOOne myDAOOne = new MyDAOOneImpl(hibernateBundle.getSessionFactory());
          MyDAOOneProxy myDAOOneProxy = new UnitOfWorkAwareProxyFactory(hibernateBundle).create(MyDAOOneProxy.class, MyDAOOneImpl.class, myDAOOne );
      
          final MyDAOTwo myDAOTwo = new MyDAOTwoImpl(hibernateBundle.getSessionFactory());
          MyDAOTwoProxy myDAOTwoProxy = new UnitOfWorkAwareProxyFactory(hibernateBundle).create(MyDAOTwoProxy.class, MyDAOTwoImpl.class, myDAOTwoProxy);
      
          //Init in memory Cache
          new CacheInitializeServiceImpl(myDAOOneProxy, myDAOTwoProxy);
      }
      

      这里的 CacheInitializeServiceImpl 类包含一个静态方法列表,用于返回存储数据库数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-20
        • 2013-09-03
        相关资源
        最近更新 更多