title: Spring Boot with Hibernate - Autowiring Repository from Another Project
body: <p>I'm attempting to set up two separate projects:</p>
  
  <ol>
  <li><strong>myApplicationWorker</strong>: backend server logic</li>
  <li><strong>myApplicationCommon</strong>: contains models and other logic that will be shared between myApplicationWorker and future projects</li>
  </ol>
  
  <p>My <strong>build.gradle</strong> dependencies for <strong>myApplicationWorker</strong> look like this:</p>
  
  <pre><code>dependencies {
      compile('org.springframework.boot:spring-boot-starter')
      compile project(':common')
      testCompile('org.springframework.boot:spring-boot-starter-test')
  }
  </code></pre>
  
  <p>and my <strong>build.gradle</strong> dependencies for <strong>myApplicationCommon</strong> look like this:</p>
  
  <pre><code>dependencies {
      compile('org.springframework.boot:spring-boot-starter-data-jpa')
      compile('mysql:mysql-connector-java')
      compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.9.1'
      compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.1'
      compile('org.apache.commons:commons-text:1.1')
      testCompile("com.h2database:h2")
      testCompile('junit:junit:5.0.2')
      testCompile('org.mockito.mockito-core:2.12.0')
  }
  </code></pre>
  
  <p>(Contains testing/mocking/logging dependencies as well as JPA/MySQL dependencies.)</p>
  
  <p>Simple hibernate/datasource properties are defined in <strong>application.properties</strong>:</p>
  
  <pre><code>spring.jpa.hibernate.ddl-auto=none
  spring.datasource.url=url
  spring.datasource.username=username
  spring.datasource.password=password
  </code></pre>
  
  <p>I have an "<strong>Account</strong>" model defined:</p>
  
  <pre><code>package com.myapplication.common.models;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "accounts")
  public class Account {
      @Id
      private long id;
  
      //Other variables, getters/setters omitted for length
  </code></pre>
  
  <p>and the Account model has an <strong>AccountRepository</strong></p>
  
  <pre><code>package com.myapplication.common.models.repositories;
  
  import com.myapplication.common.models.Account;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface AccountRepository extends CrudRepository&lt;Account, Long&gt; {
      Account findAccountById(long id);
  }
  </code></pre>
  
  <p>My goal is for any of my projects to be able to rely on the common project to interact with a backend MySQL database, simply by taking a dependency on the common project. I would expect this to work in my worker package:</p>
  
  <pre><code>package com.myapplication.worker;
  
  import com.myapplication.common.models.repositories.AccountRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.domain.EntityScan;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  
  @SpringBootApplication
  @EnableJpaRepositories(basePackages = {"com.myapplication.common"})
  @EntityScan(basePackages = {"com.myapplication.common"})
  public class Application {
      @Autowired
      private AccountRepository accountRepository;
  
      public static void main(String[] args) {
          Application application = new Application();
          application.testPrintAccountId();
      }
  
      public void testPrintAccountId() {
          System.out.println(accountRepository.findAccountbyId(1).getAccountId());
      }        
  }
  </code></pre>
  
  <p>This gives me a NPE when I try to interact with accountRepository in testPrintAccountId(), however, it (obviously) works when I manually initialize the context:</p>
  
  <pre><code>public static void main(String[] args) {
      ConfigurableApplicationContext context = SpringApplication.run(Application.class);
      AccountRepository accountRepository = context.getBean(AccountRepository .class);
      System.out.println(accountRepository .findById(1234).getAccountId());
  
      (Successfully returns result)
  }
  </code></pre>
  
  <p>I'm clearly misunderstanding how Spring and Spring Boot are supposed work here. How can I achieve my goal of being able to Autowire my repositories in my common project into other projects?</p>