title: using jar file in another project with multiple datasource
body: <p>I have spring boot application A and application B, both are using different databases. I need to  access the database A in application B to perform some calculations. So, I created a jar file from application A (which has all the POJOs) and imported into application B. Also, followed <a href="https://www.baeldung.com/spring-boot-configure-multiple-datasources" rel="nofollow noreferrer">https://www.baeldung.com/spring-boot-configure-multiple-datasources</a> to set up the configuration file.
  now, application B's database is accessible and works great after implementing configurations using above link. Though, when I am tring to access one of the POJO of application A, which tries to connect to database A, I get following error:</p>
  <blockquote>
  <p>No qualifying bean of type 'test.dao.random' available: expected at least
  1 bean which qualifies as autowire candidate. Dependency annotations:
  {@org.springframework.beans.factory.annotation.Autowired(required=true)}</p>
  </blockquote>
  <p>Following are my config file for assinging datasouce for jar file of application A :</p>
  <pre><code>import java.util.HashMap;
  
  import javax.sql.DataSource;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.boot.jdbc.DataSourceBuilder;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  
  @Configuration
  @EnableJpaRepositories(
      basePackages = {&quot;test.dao&quot;,&quot;test.daointerface&quot;}, 
      entityManagerFactoryRef = &quot;testEntityManager&quot;, 
      transactionManagerRef = &quot;testTransactionManager&quot;
  )
  public class TestConfiguration {
       
      @Autowired
      private Environment env;
  
      public TestConfiguration() {
          super();
      }
  
      @Bean(&quot;testEntityManager&quot;)
      public LocalContainerEntityManagerFactoryBean testEntityManager() {
          final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(testDataSource());
          em.setPackagesToScan(&quot;test.model&quot;);
  
          final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          final HashMap&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
          properties.put(&quot;hibernate.hbm2ddl.auto&quot;, env.getProperty(&quot;spring.jpa.hibernate.ddl-auto&quot;));
          properties.put(&quot;hibernate.dialect&quot;, env.getProperty(&quot;hibernate.dialect.second-datasource&quot;));
          em.setJpaPropertyMap(properties);
  
          return em;
      }
      
          @Bean(&quot;testDataSource&quot;)
          @ConfigurationProperties(prefix=&quot;spring.second-datasource&quot;)
          public DataSource testDataSource() {
              return DataSourceBuilder.create().build();
          }
          
          @Bean(&quot;testTransactionManager&quot;)
          public PlatformTransactionManager testTransactionManager() {
              final JpaTransactionManager transactionManager = new JpaTransactionManager();
              transactionManager.setEntityManagerFactory(testEntityManager().getObject());
              return transactionManager;
          }
      }
  </code></pre>
  <p>The POJO from Application A jar file is</p>
  <pre><code>package test.dao;
  
  import java.util.List;
  
  import org.hibernate.Hibernate;
  import org.hibernate.Query;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  import test.daointerface.IRandomDAO;
  import test.model.random;
  
  @Repository(value = &quot;randomDAO&quot;)
  @Transactional
  public class RandomDAO implements IRandomDAO
  {
      private SessionFactory sessionFactory;
  
      @Autowired
      public RandomDAO (SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
  
      @Override
      public void save(Random value)
      {
          sessionFactory.getCurrentSession().saveOrUpdate(value);
      }
  
      @Override
      public void delete()
      {
          sessionFactory.getCurrentSession().createQuery(&quot;delete from ChangedStatusReference&quot;).executeUpdate();
  
      }
  
      
  
  }
  </code></pre>
  <p>Interface class</p>
  <pre><code>package test.daointerface;
  
  import java.util.List;
  
  import test.model.Random;
  
  public interface IRandomDAO
  {
      void delete();
  
      void deleteAllLowerbyId(int id);
  
      void save(Random value);
  
  }
  </code></pre>