title: How to separate my Spring JPA configuration between numerous JARs?
body: <p>I have a use-case that I need to separate my Spring configuration into 2 completely separate applications. </p>
  
  <blockquote>
    <p>Application #1 will contain DAO models with Hibernate Mappings, JPA Repositories, DTO models, and logic to convert from a DAO to a DTO. </p>
    
    <p>Application #2 will contain the RESTful Controllers and Service classes that contain all the business logic.</p>
  </blockquote>
  
  <hr>
  
  <p><strong>Application #1</strong> configuration: </p>
  
  <pre><code>// For application startup
  package com.appone.pkg.one
  
  @SpringBootApplication
  public class Application {
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  
  // For global configuration files across numerous projects
  package com.appone.pkg.two
  
  @Configuration
  public class GlobalAppConfig {
  
      @Bean
      MapperFacade mapperFacade() {
          MapperFactory factory = new DefaultMapperFactory.Builder().build();
  
          factory.classMap(Group.class, GroupDTO.class).byDefault().register();
          factory.classMap(Role.class, RoleDTO.class).byDefault().register();
          factory.classMap(User.class, UserDTO.class).byDefault().register();
  
          return factory.getMapperFacade();
      }
   }
  
  // For configuration aspects important for 1 project in particular
  package com.appone.pkg.three
  
  @Configuration
  @EnableJpaRepositories("com.pkg.three.jpa.repository")
  public class AppTwoConfig {
  
      @Autowired
      private EntityManager em;
      private RepositoryFactorySupport factorySupport;
  
      private void initialize(){
          if(factorySupport == null){
              factorySupport = new JpaRepositoryFactory(em);
          }
      }
  
      @Bean
      GroupRepository groupRepository(){
          initialize();
          return factorySupport.getRepository(GroupRepository.class);
      }
      @Bean
      RoleRepository roleRepository(){
          initialize();
          return factorySupport.getRepository(RoleRepository.class);
      }
      @Bean
      UserRepository userRepository(){
          initialize();
          return factorySupport.getRepository(UserRepository.class);
      }
      @Bean
      GroupValidator groupValidator(){
          return new GroupValidator();
      }
      @Bean
      UserValidator userValidator(){
          return new UserValidator();
      }
      @Bean
      ResourceMapper resourceMapper(){
          return new ResourceMapper();
      }
  }
  </code></pre>
  
  <p><strong>Application #2</strong> configuration: </p>
  
  <pre><code>package com.apptwo.pkg.one;
  
  @SpringBootApplication
  @EnableAutoConfiguration
  @EntityScan(basePackageClasses = {com.appone.pkg.three.AppTwoConfig.class})
  public class ApplicationTwo {
      public static void main(String[] args) {
          SpringApplication.run(ApplicationTwo.class, args);
      }
  }
  </code></pre>
  
  <p><strong>Application #2</strong> data load: </p>
  
  <pre><code>package com.apptwo.pkg.two;
  
  @Component
  public class DataLoader implements ApplicationRunner {
  
      @Autowired
      private GroupRepository groupRepository;
      @Autowired
      private RoleRepository roleRepository;
      @Autowired
      private UserRepository userRepository;
      @Autowired
      private ResourceMapper resourceMapper;
  
      /**
       * Loads initial data into the h2 database.
       */
      public void run(ApplicationArguments args) {
          // Load the initial data ... 
      }
  }
  </code></pre>
  
  <p>This is the exception I am getting when I try to launch Application #2. Application #1 starts up fine on it's own, but I am not using the Repositories there and believe it could throw errors if I tried. Ultimately I want the logic only in Application #2 though. Do you have any ideas on what I am doing wrong?</p>
  
  <p>I need to initialize all the dependencies in Application #1 configuration and then run that configuration from inside Application #2 before I run the Application #2 configuration.</p>
  
  <blockquote>
    <p><strong>Description:</strong>
        Field groupRepository in com.apptwo.pkg.two.DataLoader required a bean of type 
        'com.appone.pkg.three.jpa.repository.GroupRepository' that could not be found.</p>
    
    <p><strong>Action</strong>:
        Consider defining a bean of type 'com.appone.pkg.three.jpa.repository.GroupRepository' in 
        your configuration.</p>
  </blockquote>