java - Spring: Pass object to RestController from Application -
so busy writing spring boot app cannot seem find out out how can pass object restcontroller main application.
here application.java:
@springbootapplication @componentscan("webservices") public class application { public static void main(string[] args) { applicationcontext ctx = springapplication.run(application.class, args); application app = new application(ctx); linkedblockingqueue<rawdate> queue = new linkedblockingqueue<>(); // other stuff here } }
and here restcontroller:
@restcontroller public class googletokencontroller { private linkedblockingqueue<rawdata> queue; @crossorigin @requestmapping(value = "/google/token", method = requestmethod.post, headers = {"content-type=application/json"}) @responsebody public string googletoken(@requestbody authcode authcode) { system.out.println("code: " + authcode.getauthcode()); // other stuff here return "ok"; } }
so want pass same instance of linkedblockingqueue<rawdata>
created in application
class googletokencontroller
class. have no idea how since spring automatically creates googletokencontroller
class.
please note new spring. thanks.
make object want pass spring bean , let spring inject controller. example:
@springbootapplication @componentscan("webservices") public class application { @bean public linkedblockingqueue<rawdate> queue() { return new linkedblockingqueue<>(); } public static void main(string[] args) { applicationcontext ctx = springapplication.run(application.class, args); application app = new application(ctx); // other stuff here } } @restcontroller public class googletokencontroller { @autowired // let spring inject queue private linkedblockingqueue<rawdata> queue; @crossorigin @requestmapping(value = "/google/token", method = requestmethod.post, headers = {"content-type=application/json"}) @responsebody public string googletoken(@requestbody authcode authcode) { system.out.println("code: " + authcode.getauthcode()); // other stuff here return "ok"; } }
in other places need access queue
, let spring inject it.
Comments
Post a Comment