title: SpringBoot: Can't Autowire Class from Other Jar Library
body: <p>I am developing a SpringBoot application (e.g. MyApp) with dependency to two data projects with different implementation:</p>
  
  <blockquote>
    <p>data-jdbc.jar</p>
  </blockquote>
  
  <ul>
  <li>built using the <code>spring-boot-starter-jdbc</code> which exposes JDBCDataService class that will be used by my application</li>
  </ul>
  
  <p>Sample Code:</p>
  
  <pre><code>@Service 
  public class JDBCDataServiceImpl implements JDBCDataService {
  
  @Autowired
  private JDBCDataRepository jdbcDataRepository;    
  ... 
  }
  </code></pre>
  
  <ul>
  <li>with package <code>my.data.jdbc</code></li>
  <li>there is no SpringBoot main class. Spring configuration only created for the unit test classes</li>
  <li>the repository classes are using <code>JDBCTemplate</code></li>
  </ul>
  
  <p>Sample Repository:</p>
  
  <pre><code>@Repository
  public class JDBCDataRepositoryImpl implements JDBCDataRepository {
  
  @Autowired
  protected JdbcTemplate jdbcTemplate;
  ...
  }
  </code></pre>
  
  <blockquote>
    <p>data-jpa.jar</p>
  </blockquote>
  
  <ul>
  <li>built using the <code>spring-boot-starter-data-jpa</code> which also exposes JPADataService class that will also be used by my application</li>
  </ul>
  
  <p>Sample Code:</p>
  
  <pre><code>@Service 
  public class JPADataServiceImpl implements JPADataService {
  
  @Autowired
  private JPADataRepository jpaDataRepository;    
  ... 
  }
  </code></pre>
  
  <ul>
  <li>with package <code>my.data.jpa</code></li>
  <li>there is no SpringBoot main class. Spring configuration only created for the unit test classes</li>
  <li>repository classes extends the <code>CrudRepository</code> interface</li>
  </ul>
  
  <p>Sample Repository:</p>
  
  <pre><code>@Repository
  public interface JPADataRepository extends CrudRepository&lt;MyObject, Integer{
  ...
  }
  </code></pre>
  
  <p>In my SpringBoot project, I have the following SpringBoot main application:</p>
  
  <pre><code>@SpringBootApplication
  public class MyApp extends SpringBootServletInitializer {
  }
  </code></pre>
  
  <p>In my business service <code>MainService</code> class, I have the following injection </p>
  
  <pre><code>@Service
  public class MainServiceImpl implements MainService {
  
  @Autowired
  private JDBCDataService jdbcDataService;
  
  @Autowired
  private JPADataService jpaDataService;
  </code></pre>
  
  <p>However, I have encountered the problem <code>"Could not Autowire. No beans of 'JPADataService' type found"</code> which only exists for the class <code>JPADataService</code> but working fine for <code>JDBCService</code> class.</p>
  
  <p>I have tried the solution found in the following questions, but none of these work in my case:</p>
  
  <p><a href="https://stackoverflow.com/questions/30796306/cant-i-autowire-a-bean-which-is-present-in-a-dependent-library-jar">Can't I @Autowire a Bean which is present in a dependent Library Jar?</a></p>
  
  <pre><code>@ComponentScan(basePackages = {"org.example.main", "package.of.user.class"})
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/29571304/how-can-i-autowire-a-spring-bean-that-was-created-from-an-external-jar">How can I @Autowire a spring bean that was created from an external jar?</a></p>
  
  <pre><code>@Configuration
  @ComponentScan("com.package.where.my.class.is")
  class Config {
  ...
  }
  </code></pre>
  
  <p>I have now found the solution on my problem. I have to move up my main MyApp.java one package level higher in order to scan my data libraries.</p>
  
  <p>Instead of putting my <code>MyApp.java</code> under <code>my.app</code> package, I have to move it under <code>my</code> in order to successfully scan my libraries with <code>my.data.jpa</code> and <code>my.data.jdbc</code> packages.</p>