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.
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.
Right now we're starting that control flow here.
@SpringBootApplication
@EnableWebSecurity
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SSHParser parser = new SSHParser();
parser.startParserLoop();
}
}
In the RestController we want to have a reference to that parser object so we can use it in the HTTP request methods.
@RestController
public class RestController {
@Autowire
SSHParser parser;
@RequestMapping("/api/list")
public Entry[] getList() {
return parser.list();
}
}
I think I understand we could do something like the above, but this creates a new instance of SSHParser
when what we really need is the instance parser
from the main
method.
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?