title: Spring RestController interacting with other Java classes
body: <p>I'm quite new to Spring.  I've used it before to make a REST API and I'm using it again now for the same purpose.</p>
  
  <p>In addition to hosting the REST service, this program also repeatedly opens a file, scans for patterns, and maintains a structure of what it finds.</p>
  
  <p>Right now we're starting that control flow here.</p>
  
  <pre><code>@SpringBootApplication
  @EnableWebSecurity
  public class Application {
  
    public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
      SSHParser parser = new SSHParser();
      parser.startParserLoop();
    }
  }
  </code></pre>
  
  <p>In the RestController we want to have a reference to that parser object so we can use it in the HTTP request methods.</p>
  
  <pre><code>@RestController
  public class RestController {
  
    @Autowire
    SSHParser parser;
  
    @RequestMapping("/api/list")
    public Entry[] getList()  {
      return parser.list();
    }
  }
  </code></pre>
  
  <p>I think I understand we could do something like the above, but this creates a new instance of <code>SSHParser</code> when what we really need is the instance <code>parser</code> from the <code>main</code> method.</p>
  
  <p>Is this something we're just not meant to do with the Spring framework?  Would it be possible to call the constructor for the rest controller ourselves to pass a reference that way?</p>