title: Spring startup performance issues
body: <p>I'm trying to integrate Spring in a pretty large application with thousands of classes, and i'm experiencing huge delays starting my container because of component-scanning.</p>
  
  <p>I have already narrowed the number of directories specified in the "base-package", to the minimum in order to reduce the time wasted in scanning irrelevant directories, but the class-path scanning part of initialization still takes about 1-2 mins.</p>
  
  <p>So, is there a way to optimize the scanning process ? I've thought of storing the candidate classes path in a file and make the container then get them from the file instead of scanning the class-path with every startup, but i don't really know where to start or if that is even possible.</p>
  
  <p>Any advice is much appreciated. Thanks in advance.  </p>
  
  <hr>
  
  <p><strong>Edit1</strong>: Loading bean definitions form an autogenerated xml file, reduced the Spring bootstrap time to 9~10 secs which confirms that the reflection api used by Spring for the components class-path scanning is the major source of startup delays.<br>
  As for generating the xml file here is the code, since it might be helpful for someone with the same issues.</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  
  
  public class ConfigurationWriter {
  
      public ArrayList&lt;String&gt; beanDefinitions = new ArrayList&lt;String&gt;();
  
      public ConfigurationWriter() {
  
          // the context loaded with old fashioned way (classpath scanning)
          ApplicationContext context = SpringContainerServiceImpl.getInstance().getContext();
          String[] tab = context.getBeanDefinitionNames();
          for (int i = 0; i &lt; tab.length - 6; i++) {
              Class clazz = context.getType(tab[i]);
              String scope = context.isPrototype(tab[i]) ? "prototype" : "singleton";
              String s = "&lt;bean id=\"" + tab[i] + "\" class=\"" + clazz.getName() + "\" scope=\"" + scope + "\"/&gt;";
              beanDefinitions.add(s);
          }
          // Collections.addAll(beanDefinitions, tab);
  
      }
  
      @SuppressWarnings("restriction")
      public void generateConfiguration() throws FileNotFoundException {
          File xmlConfig = new File("D:\\dev\\svn\\...\\...\\src\\test\\resources\\springBoost.xml");
          PrintWriter printer = new PrintWriter(xmlConfig);
  
          generateHeader(printer);
  
          generateCorpse(printer);
  
          generateTail(printer);
  
          printer.checkError();
  
      }
  
      @SuppressWarnings("restriction")
      private void generateCorpse(PrintWriter printer) {
  
          for (String beanPath : beanDefinitions) {
              printer.println(beanPath);
          }
  
      }
  
      @SuppressWarnings("restriction")
      private void generateHeader(PrintWriter printer) {
          printer.println("&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;");
          printer.println("&lt;beans xmlns=\"http://www.springframework.org/schema/beans\"");
          printer.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
          printer.println("xmlns:context=\"http://www.springframework.org/schema/context\"");
          printer.println("xsi:schemaLocation=\"");
          printer.println("http://www.springframework.org/schema/mvc");
          printer.println("http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd");
          printer.println("http://www.springframework.org/schema/beans");
          printer.println("http://www.springframework.org/schema/beans/spring-beans-3.0.xsd");
          printer.println("http://www.springframework.org/schema/context");
          printer.println("http://www.springframework.org/schema/context/spring-context-3.0.xsd\"");
          printer.println("default-lazy-init=\"true\"&gt;");
      }
  
      @SuppressWarnings("restriction")
      private void generateTail(PrintWriter printer) {
          // printer.println("&lt;bean class=\"com.xxx.frmwrk.spring.processors.xxxBeanFactoryPostProcessor\"/&gt;");
          printer.println("&lt;bean class=\"com.xxx.frmwrk.spring.processors.xxxPostProcessor\"/&gt;");
          printer.println("&lt;/beans&gt;");
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit 2:</strong> With Spring 5 including an important set of optimizations for speeding up the context initialization, It also comes with an interesting and handy feature that enables generating an index of candidate components at compile time : <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-scanning-index" rel="nofollow noreferrer">Spring Context Indexer</a>  </p>