title: spring bean startup/shutdown order configuration (start h2 db as server)
body: <p>I'd like to create configuration/bean to automatically start H2DB in my development profile. I'd like to have it running as a tcp server. It's needed to be started before any DataSource configuration. Can someone tell me how to achieve this?</p>
  
  <p>Wha have I done is </p>
  
  <pre><code>@Profile("h2")
  @Component
  public class H2DbServerConfiguration implements SmartLifecycle {
  
      private static final Logger logger = LoggerFactory.getLogger(H2DbServerConfiguration.class);
  
      private Server server;
  
      @Override
      public boolean isAutoStartup() {
          return true;
      }
  
      @Override
      public void stop(Runnable callback) {
          stop();
          new Thread(callback).start();
      }
  
      @Override
      public void start() {
          logger.debug("############################################");
          logger.debug("############################################");
          logger.debug("STARTING SERVER");
          logger.debug("############################################");
          logger.debug("############################################");
          try {
              server = Server.createTcpServer("-web", "-webAllowOthers", "-webPort", "8082").start();
          } catch (SQLException e) {
              throw new RuntimeException("Unable to start H2 server", e);
          }
      }
  
      @Override
      public void stop() {
          logger.debug("############################################");
          logger.debug("############################################");
          logger.debug("STOPPING SERVER");
          logger.debug("############################################");
          logger.debug("############################################");
          if (server != null)
              if (server.isRunning(true))
                  server.stop();
      }
  
      @Override
      public boolean isRunning() {
          return server != null ? server.isRunning(true) : false;
      }
  
      @Override
      public int getPhase() {
          return 0;
      }
  }
  </code></pre>
  
  <p>but this isn't an option for me because component is created after datasource (I have liquibase setup so it's too late) and Phase is still the same that means FIFO order and I'd like to be FILO.</p>