title: How to access global service objects (injection-like)?
body: <p>I am rather new to flask but what I mean by the title is I want to create a service object which can be used in all controllers or services. </p>
  
  <p>In Java (Spring) I would create a <code>@Bean</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  public class WhateverGatewayBeans {
  
      @Value("{whatever.api.key}")
      public String apiKey;
  
      @Bean
      public WhateverGateway getWhateverGateway() {
          return new WhateverGateway(apiKey)
      }
  }
  </code></pre>
  
  <p>Which I can now use in all my <code>@Service</code>s:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class AnyService {
  
      private final WhateverGateway whateverGateway;
  
      @Autowired // Using injection
      public AnyService(WhateverGateway whateverGateway) {
          this.whateverGateway = whateverGateway;
      }
  
      public Something getSomethingFromWhatever(String param) {
          return this.whateverGateway.getSomething(param);
      }
  }
  </code></pre>
  
  <p>And so on</p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  public class AnyController {
  
      private final AnyService anyService;
  
      @Autowired
      public AnyController(AnyService anyService) {
          this.anyService = anyService;
      }
  
      @RequestMapping(name = "/api/whatever")
      public Something getSomething(@QueryParam("param") String param) {
          return this.anyService.getSomething(param);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Please note that the above code was written on the fly and likely to be not 100% correct but you get the idea</p>
  </blockquote>
  
  <p>I am looking for something similar in Flask:</p>
  
  <pre><code>@app.route('/api/something')
  def get_something(whatever_gateway):
      param = request.args.get('param')
      return whatever_gateway.get_something_from_whatever(param)
  </code></pre>
  
  <p>Here, <code>whatever_gateway</code> should be fully initialized during the server startup. Imagine it has to load for some time, the server should not be up before <code>whatever_gateway</code> is ready.</p>