title: How do I autowire 3rd party classes with annotations in Spring?
body: <p>The majority of examples on Spring Boot I've found focus on building simple web applications. That is, web applications where you are in complete and utterly control of everything.</p>
  
  <p>On the other side, I have not had much luck in finding examples on how to build non-web applications where larger parts of the application depends on 3rd party code. </p>
  
  <p>Consider my <code>com.mypackage.Application</code> class below.</p>
  
  <pre><code>package com.mypackage;
  
  import com.3rdparty.factory.ServiceFactory;
  
  public class Application {
  
    private final ServiceFactory sf;
  
    public Application(ServiceFactory sf) {
      this.sf = sf;
    }
  
    public void doSomeWork() {
      ServiceA sa = sf.getServiceA();
      [...]
  }
  </code></pre>
  
  <p>The <code>Application</code> class simply instantiates <code>DefaultManager</code> and invokes <code>run()</code>.</p>
  
  <p>Now, the 3rd party <code>ServiceFactory</code> class has additional dependencies:</p>
  
  <pre><code>package com.3rdparty.factory;
  
  import com.3rdparty.service.ServiceA;
  import com.3rdparty.service.ServiceA;
  
  public class ServiceFactory {
  
    private final ServiceA sa;
    private final ServiceB sb;
  
    public ServiceFactory(ServiceA sa, ServiceB sb) {
      this.sa = sa;
      this.sb = sb;
    }
  
    public ServiceA getServiceA() {
      return sa;
    }
  
    public ServiceB getServiceB() {
      return sb;
    }
  
  }
  </code></pre>
  
  <p>I could launch <code>Application</code> from a <code>Main</code> class:</p>
  
  <pre><code>import com.mypackage.Application;
  
  public class Main {
  
    public static void main(String[] args) { 
      ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml");
      Application app = (Application) context.getBean("app");
      app.doSomeWork();
    }
  </code></pre>
  
  <p><strong>Question</strong> : how do I inject <code>ServiceA</code> and <code>ServiceB</code> into <code>ServiceFactory</code>. This is a 3rd party class, I have no control over it, and I can't modify it. Thus, I can't add any annotations. </p>
  
  <p>I can easily get this to work with XML configuration, but considering that annotations seems to be the "best practise" way of doing it these days I'd like to know how I can get this to work with annotations. </p>
  
  <p>If the annotation way of doing this involves a large amount of code, then I'd like to know what advantages this gives me compared to the XML configuration which I think is quite easy to comprehend; and a pattern which is easy to get going with across different projects.</p>