title: Integrate Spring boot into existing spring jar standalone
body: <p>I have problems integrating spring boot into my existing spring project. There are tree questions I have, but before I come to these, let me explain my current architecture:  </p>
  
  <p><strong>Current project</strong> </p>
  
  <p>I have two projects. The first one is called <code>myProject.toolkit</code> and has only a main class in witch I implemented an endless do-while loop. This loop creates a <code>ProcessBuilder</code> in order to start a new process. And when this process terminates it checks whether or whether not this exit was wanted or not. If so, it terminates. If not, it starts the same process again (not very beautiful, but I am still learning and maybe I have an better idea in the future).<br>
  The second project is called <code>myProject.dependencies</code> and this is the program that will be started from the toolkit inside the loop. This is also the real project because the toolkit only has the purpose to monitor over it.  </p>
  
  <p>Both projects are maven projects that are compiled to standalone jar files using aspectJ.<br>
  <code>myProject.dependencies</code> has sprint boot, hibernate and jpa. Everything is working fine with my MySQL database. I can create Services, that will use Repositories in order to persistant Entities.  </p>
  
  <p>For this I created a class called <code>JpaConfig</code> that has all the needed configurations in it because xml files didn't work. It looks shortened like this:  </p>
  
  <pre><code>@EnableScheduling
  @EnableAsync
  @EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
  @ComponentScan(basePackages = {"myProject"})
  @Configuration
  @EnableJpaRepositories
  public class JpaConfig { 
  
      @Bean
      public InstrumentationLoadTimeWeaver loadTimeWeaver(){
          return new InstrumentationLoadTimeWeaver();
      }
  
      @Bean
      public DataSource dataSource(){
          // creates, configures and returnes a "BoneCPDataSource" Object
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
          // creates, configures and returnes a "LocalContainerEntityManagerFactoryBean" Object
      }
  
      @Bean
      public JpaTransactionManager transactionManager(){
          // creates, configures and returnes a "JpaTransactionManager" Object
      }
  
  }
  </code></pre>
  
  <p>In my main class of the <code>myProject.dependencies</code> project I call one magic line in order to get spring working:  </p>
  
  <pre><code>AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JpaConfig.class);
  </code></pre>
  
  <p>With this line the configuration gets loaded and I can use hibernate, jpa and spring with all its functionality.  </p>
  
  <p><strong>integrating spring boot</strong>  </p>
  
  <p>Now I want to add spring boot to the project because I need a REST API. And because I read that spring boot can run without a tomcat or glassfish and also within a jar and not a war, I thought this is the perfect architecture for me.  </p>
  
  <p>So I read some articles in order to verify this and on the point that I found out spring boot brings an own integrated tomcat, I tried to implement it in my existing project.  </p>
  
  <p>I added the following dependencies to my <code>pom.xml</code>:  </p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;version&gt;1.4.1.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; 
          &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt; 
          &lt;version&gt;1.4.1.RELEASE&lt;/version&gt;
          &lt;!--&lt;scope&gt;provided&lt;/scope&gt;--&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;version&gt;1.4.1.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>And because I could not set a parent pom (I already have a parent set and multiple parents are not possible in maven), I also added this to my pom without understanding it, but since these lines are also in <code>org.springframework.boot:spring-boot-starter-parent:1.4.1.RELEASE</code> and everyone told me to rely on this configuration, I added it:  </p>
  
  <pre><code>    &lt;pluginManagement&gt;
          &lt;plugins&gt;
              &lt;!-- Apply more sensible defaults for user projects --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.19.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;integration-test&lt;/goal&gt;
                              &lt;goal&gt;verify&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;archive&gt;
                          &lt;manifest&gt;
                              &lt;mainClass&gt;${start-class}&lt;/mainClass&gt;
                              &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;
                          &lt;/manifest&gt;
                      &lt;/archive&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;includes&gt;
                          &lt;include&gt;**/*Tests.java&lt;/include&gt;
                          &lt;include&gt;**/*Test.java&lt;/include&gt;
                      &lt;/includes&gt;
                      &lt;excludes&gt;
                          &lt;exclude&gt;**/Abstract*.java&lt;/exclude&gt;
                      &lt;/excludes&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                      &lt;archive&gt;
                          &lt;manifest&gt;
                              &lt;mainClass&gt;${start-class}&lt;/mainClass&gt;
                              &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;
                          &lt;/manifest&gt;
                      &lt;/archive&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;mainClass&gt;${start-class}&lt;/mainClass&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.6&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;delimiters&gt;
                          &lt;delimiter&gt;${resource.delimiter}&lt;/delimiter&gt;
                      &lt;/delimiters&gt;
                      &lt;useDefaultDelimiters&gt;false&lt;/useDefaultDelimiters&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;pl.project13.maven&lt;/groupId&gt;
                  &lt;artifactId&gt;git-commit-id-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.1.11&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;revision&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
                  &lt;configuration&gt;
                      &lt;verbose&gt;true&lt;/verbose&gt;
                      &lt;dateFormat&gt;yyyy-MM-dd'T'HH:mm:ssZ&lt;/dateFormat&gt;
                      &lt;generateGitPropertiesFile&gt;true&lt;/generateGitPropertiesFile&gt;
                      &lt;generateGitPropertiesFilename&gt;${project.build.outputDirectory}/git.properties&lt;/generateGitPropertiesFilename&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;!-- Support our own plugin --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.4.1.RELEASE&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;repackage&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
                  &lt;configuration&gt;
                      &lt;mainClass&gt;${start-class}&lt;/mainClass&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;!-- Support shade packaging (if the user does not want to use our plugin) --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.4.3&lt;/version&gt;
                  &lt;dependencies&gt;
                      &lt;dependency&gt;
                          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                          &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                          &lt;version&gt;1.4.0.BUILD-SNAPSHOT&lt;/version&gt;
                      &lt;/dependency&gt;
                  &lt;/dependencies&gt;
                  &lt;configuration&gt;
                      &lt;keepDependenciesWithProvidedScope&gt;true&lt;/keepDependenciesWithProvidedScope&gt;
                      &lt;createDependencyReducedPom&gt;true&lt;/createDependencyReducedPom&gt;
                      &lt;filters&gt;
                          &lt;filter&gt;
                              &lt;artifact&gt;*:*&lt;/artifact&gt;
                              &lt;excludes&gt;
                                  &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;
                                  &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;
                                  &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;
                              &lt;/excludes&gt;
                          &lt;/filter&gt;
                      &lt;/filters&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;shade&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;transformers&gt;
                                  &lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
                                      &lt;resource&gt;META-INF/spring.handlers&lt;/resource&gt;
                                  &lt;/transformer&gt;
                                  &lt;transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer"&gt;
                                      &lt;resource&gt;META-INF/spring.factories&lt;/resource&gt;
                                  &lt;/transformer&gt;
                                  &lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
                                      &lt;resource&gt;META-INF/spring.schemas&lt;/resource&gt;
                                  &lt;/transformer&gt;
                                  &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /&gt;
                                  &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                                      &lt;mainClass&gt;${start-class}&lt;/mainClass&gt;
                                  &lt;/transformer&gt;
                              &lt;/transformers&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/pluginManagement&gt;
  </code></pre>
  
  <ol>
  <li>Do I need all this plugins? And a <code>pluginManagement</code> inside a pom without heredity ... unnecessary? What do I need from it?   </li>
  </ol>
  
  <p>After finishing the <code>pom.xml</code> I modified the main class and added a second line to it:  </p>
  
  <pre><code>AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JpaConfig.class);
  ApplicationContext ctx = SpringApplication.run(SpringBootApp.class, args);
  </code></pre>
  
  <ol start="2">
  <li><p>There are now two contexts. It can start, yes, but I am not sure if this is useful. I do not want to configure everything twice. So how can I modify my <code>JpaConfig</code> Class in order to also configure spring boot. And going on: how can I modify my two lines in my main class in order to make the configuration only once? Maybe use <code>SpringApplication.run</code> and give it the <code>JpaConfig</code> Class? But the constructor is not allowing this.  </p></li>
  <li><p>When I start this current project with all the modifications, independently that maybe hibernate and/or jpa and/or spring annotations are searched and bild twice, I get this error:  </p>
  
  <p>org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [myProject.dependency.spring.javaOnly.springBoot.SpringBootApp]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
  // ...
  Caused by: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.</p></li>
  </ol>
  
  <p>I am not quite sure what this is telling me because I created a <code>spring.factories</code> in <code>META-INF</code> and when I compile it, there are even two <code>spring.factories</code> inside the <code>META-INF</code> folder. The one I created myself and one created by the compiler. And regardless of whether I have two or only one <code>spring.factories</code> inside the <code>META-INF</code> folder, the error still is the same.<br>
  But maybe this one will fix itself when the other two questions are solved. So thank you so much for your time and patience for this long post and my maybe stupid questions. But I hope you understand that I am still learning and trying my best to improve and these problems are unsolvable for me since a few days. So thanks for any hint or/and help.</p>