title: Deep Beans initialization with thread-specific data in Spring framework
body: <p>Spring's dependency injection mechanism looks convenient because I don't have to create classes manually from root class and pass them to deeper classes. But what if I have 3 separate threads which do the same job and have some thread-specific data which should be spread through the class hierarchy?</p>
  
  <p>Consider the following App:</p>
  
  <pre><code>@SpringBootApplication
  public class App {
  
      public static void main(String[] args) throws IOException {
          Properties props = new Properties(); 
          props.load(App.class.getResourceAsStream("/application.properties"));
  
          String[] domains = props.getProperty("domains").split(",");
  
          ConfigurableApplicationContext context = SpringApplication.run(App.class);
          ConfigurableListableBeanFactory factory = context.getBeanFactory();
  
          for(String domain : domains) {
              DomainListener listener = factory.getBean(DomainListener.class);
              listener.setDomain(domain);
              listener.start();
          }
      }
  
  }
  
  @Service
  @Scope("prototype")
  public class DomainListener extends Thread {
  
      @Autowired
      EventPublisher publisher;
  
      String domain;
  
      public void setDomain(String domain) {
          this.domain = domain;
      }
  
      @Override
      public void run() {
          System.out.println("starting '" + domain +  "' domain thread...");
  
          publisher.setDomain(domain); // &lt;-- is this the best way to initialize bean?
  
          // some processing
  
          publisher.publish("Some event");
      }
  
  }
  
  @Component
  @Scope("prototype")
  public class EventPublisher {
  
      String domain;
  
      public void setDomain(String domain) {
          this.domain = domain;
      }
  
      public void publish(String msg) {
          System.out.println("publishing '" + msg + "' to '" + domain + "' domain");
      }
  
  }
  </code></pre>
  
  <p><code>application.properties</code> file content</p>
  
  <pre><code>domains = domain1.example.com,domain2.example.com,domain3.example.com
  </code></pre>
  
  <p><code>DomainListener</code> and <code>EventPublisher</code> classes can't be initialized with <code>domain</code> value automatically by Spring, thus I have to call <code>setDomain</code> method and pass the domain value manually, first during <code>DomainListener</code> thread construction, then in <code>DomainListener.run()</code> to initialize <code>EventPublisher</code> with domain value. Would I need to pass the value even deeper, for example from <code>EventPublisher</code> to some other class, say <code>MessageBuilder</code> (let's assume it requires domain value), then I would have to call <code>MessageBuilder.setDomain()</code> instance method from <code>EventPublisher</code> constructor (as it is the same domain thread). </p>
  
  <p>Can this be improved somehow?</p>