title: Invoke request to localhost immediately after servlet container startup
body: <p>I'm looking to have my java web application call itself at a localhost URL immediately after the servlet container (be it tomcat, jetty, ...) begins accepting requests. </p>
  
  <p>I'm using the java spring framework but I believe that's a "side issue" since it's really the servlet container's status that I need to be aware of. </p>
  
  <p>As I understand it, spring initializes the application context with all its beans first, then maps the URLs and initializes the <code>DispatcherServlet</code> to handle the handling/filtering of requests.</p>
  
  <p>I'm looking to find the "moment" when I can safely use a <code>RestTemplate</code> to call the server itself. Everything I've tried seems to be "too early" as it has resulted in <code>java.net.ConnectException: Connection refused</code>--except for when I invoke it manually from a web browser via a controller endpoint--which succeeds.</p>
  
  <p>I've tried using:</p>
  
  <ol>
  <li><code>javax.servlet.ServletContextListener</code> as per <a href="https://stackoverflow.com/questions/10484186/how-to-invoke-a-method-on-a-servlet-or-controller-after-the-web-container-has-su#comment13554634_10484828">How to invoke a method on a servlet or controller after the web container has successfully started</a></li>
  <li><code>org.springframework.context.ApplicationListener&lt;ContextRefreshedEvent&gt;</code></li>
  <li>a hacky custom servlet's init method with a "load-on-startup" after the <code>DispatcherServlet</code></li>
  </ol>
  
  <p>At some point in time, the servlet container must have to take over from spring's setup and "flip the switch" to "on". Also, I'd like to do this in a servlet container "agnostic" way so that I don't have specific tomcat/jetty code.</p>
  
  <p>Here's the resttemplate exception. I'm running the app on port 9090 and my contextPath is 'openid-connect-provider'. 'foo' is a very simple GET endpoint that works, as I mentioned, when invoked after the servlet container is started.</p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:9090/openid-connect-provider/foo": Connection refused; nested exception is java.net.ConnectException: Connection refused
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:582)
  ...
  </code></pre>
  
  <p>The only other thing I can say is that these errors occur in my logs/console just before Jetty tells me that it's started:</p>
  
  <pre><code>[INFO] Started ServerConnector@1bcba9c7{HTTP/1.1,[http/1.1]}{0.0.0.0:9090}
  [INFO] Started @13749ms
  [INFO] Started Jetty Server
  </code></pre>
  
  <p>[update]
  A bit more background. I'm implementing an OAuth2 Authorization Server (AS). My user credentials are in a separate database that I need to access through a separate service (which is an oauth2 resource server or RS). I need my AS to call the RS to authenticate users but I want to protect my RS with tokens granted by my AS. So, i need to setup my AS as a oauth2 client of itself so that it can call the RS securely. To do that, I want to dynamically register the application (with itself) to get client_id/client_secret credentials generated so that it can call my RS. Ideally, I'd want my user info to be in the same service and not to have to do this but this is a 6-month interim step.</p>