简体   繁体   中英

Spring: Pass object to RestController from Application

So I am busy writing a Spring Boot app but I cannot seem to find out out how I can pass an object to my RestController from my main application.

Here is my 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<>();
        // do other stuff here
    }
}

And here is my 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());
        // do other stuff here
        return "OK";
    }
}

So I want to pass the same instance of LinkedBlockingQueue<RawData> that is created in the Application class to the GoogleTokenController class. But I have no idea how to do that since spring automatically creates the GoogleTokenController class.

Please note I am very new to Spring. Thanks.

Make the object that you want to pass a Spring bean and let Spring inject it into the controller. For 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);
        // do other stuff here
    }
}

@RestController
public class GoogleTokenController {
    @Autowired // Let Spring inject the 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());
        // do other stuff here
        return "OK";
    }
}

In other places where you need access to queue , also let Spring inject it.

How about creating a component that you can inject into your controller? You could create a seperate class for the Queue as

@Component
public class RawDateQueue extends LinkedBlockingQueue<RawDate> {
    // no further implementations
}

and use RawDateQueue in your Controller.

use -:

  @autowired 
   private LinkedBlockingQueue<RawData> queue;

    OR

   @inject 
   private LinkedBlockingQueue<RawData> queue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM