title: Spring Boot Rest Service Bean Creation Exception
body: <p>I am getting the below exception in my Spring boot rest service Application.This exception occurs sometimes when I start the service.I am able to start the service after 4 or 5 attempts.Any solution is appreciated.</p>
  
  <pre><code>RuntimeException generated in Method : org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBeanand exception messageError creating bean with name 'changeRecordService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.att.vtm.changerecord.services.changerecord.wrapper.IChangeRecordAdapter com.att.vtm.changerecord.services.changerecord.ChangeRecordService.iChangeRecordAdapter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'injectChangeRecordDaoAdapter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.att.vtm.changerecord.dao.VTMRefUserUpdateHistoryRepository com.att.vtm.changerecord.services.changerecord.wrapper.ChangeRecordDaoAdapter.vTMRefUserUpdateHistoryRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.att.vtm.changerecord.dao.VTMRefUserUpdateHistoryRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>
  
  <p><strong>This is my Spring Boot Main class</strong></p>
  
  <pre><code>@SpringBootApplication(exclude = { HypermediaAutoConfiguration.class })
  @EnableSwagger2
  @EnableCaching
  public class Application {
  
      private static String adapterType;
  
      public static String getAdapterType() {
          return adapterType;
      }
  
      public static void setAdapterType(String adapterType) {
          Application.adapterType = adapterType;
      }
  
      public static void main(String[] args) {
  
          if (args.length != 0) {
              Application.setAdapterType(args[0]);
  
          } else {
              Application.setAdapterType("DAO");
          }
  
          try {
  
              SpringApplication.run(Application.class, args);
  
          } catch (Exception runtimeException) {
              System.out.println(
                      "RuntimeException generated in Method : " + runtimeException.getStackTrace()[1].getClassName() + "#"
                              + runtimeException.getStackTrace()[1].getMethodName() + "and exception message"
                              + runtimeException.getMessage());
              // Runtime.getRuntime().addShutdownHook(hook);
          }
      }
  
      /**
       * Tweak the connection pool used by Jetty to handle incoming HTTP
       * minThreads &amp; maxThreads connections
       * 
       * @param port
       * @param maxThreads
       * @param minThreads
       * @param idleTimeout
       * @return
       */
      @Bean
      public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(
              @Value("${server.port}") final String port,
              @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads,
              @Value("${jetty.threadPool.minThreads:50}") final String minThreads,
              @Value("${jetty.threadPool.idleTimeout:60000}") final String idleTimeout) {
  
          final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(
                  Integer.valueOf(port));
          factory.addServerCustomizers(new JettyServerCustomizer() {
              public void customize(Server server) {
                  final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
                  threadPool.setMaxThreads(Integer.valueOf(maxThreads));
                  threadPool.setMinThreads(Integer.valueOf(minThreads));
                  threadPool.setIdleTimeout(Integer.valueOf(idleTimeout));
              }
  
          });
          return factory;
      }
  
      @Bean
      public Docket newsApi() {
          return new Docket(DocumentationType.SWAGGER_2).groupName("changerecord").apiInfo(apiInfo()).select()
                  .paths(regex("/bsi/.*")).build().directModelSubstitute(XMLGregorianCalendar.class, MixIn.class);
      }
  
      public static interface MixIn {
          @JsonIgnore
          public void setYear(int year);
      }
  
      /**
       * 
       * @return
       */
      private ApiInfo apiInfo() {
          return new ApiInfoBuilder().title("Catalog Service REST APIs").description("Microservice REST APIs")
                  .termsOfServiceUrl("http://....").contact("TechMahindra").license("TechMahindra Licensed")
                  .licenseUrl("https://techmahindra.com").version("2.0").build();
      }
  }
  </code></pre>
  
  <p><strong>This is my controller class.</strong>  </p>
  
  <pre><code>@RestController
  @RequestMapping("/bsi/vtm/changerecord")
  public class ChangeRecordController {
  
      @Autowired
      private ChangeRecordService changerecordService;
  
      private static final Logger LOG = LoggerFactory.getLogger(ChangeRecordController.class);
  </code></pre>
  
  <p><strong>This is my service class.</strong></p>
  
  <pre><code>@org.springframework.stereotype.Service
  public class ChangeRecordService {
              private static final Logger LOG = LoggerFactory.getLogger(ChangeRecordService.class);
  
  @Autowired
  private IChangeRecordAdapter iChangeRecordAdapter;
  </code></pre>
  
  <p><strong>This is Change Record Impl</strong></p>
  
  <pre><code>public class ChangeRecordDaoAdapter extends IChangeRecordAdapter {
  
      private static final Logger LOG = LoggerFactory.getLogger(ChangeRecordDaoAdapter.class);
  
      @Autowired
      private VTMRefUserUpdateHistoryRepository vTMRefUserUpdateHistoryRepository;
  
      @Autowired
      private NotificationHelper notificationHelper;  
  
      @Autowired
      private SearchRepository searchRepository;
  
      @Autowired
      private CounterRepository counterRepository;
  
      @Autowired
      private ProducerConfiguration producerConfiguration;
  
  
      @Autowired
      private ChangeRecordRepository changerecordRepository;
  </code></pre>
  
  <p><strong>This is my Notification helper</strong></p>
  
  <pre><code>package com.att.vtm.changerecord.services.changerecord.wrapper;
  
      public class NotificationHelper {
  
      private static final Logger LOG = LoggerFactory.getLogger(NotificationHelper.class);
      @Autowired
      private ChangeRecordRepository changeRecordRepository;
  
      @Autowired
      private ImpactedGroupsSearchCriteria impactedGroupsSearchCriteria;
  
      @Autowired
      private TechnologiesSearchCriteria technologiesSearchCriteria;
  
      @Autowired
      private SearchGroupRepository searchGroupRepository;
  
      @Autowired
      private CbusEmailClient client;
  
      @Autowired
      private NotificationTrackingRepository notificationTrackingRepository;
  
      @Autowired
      private VTMRefUserUpdateHistoryRepository userUpdateHistoryRepository;
  
  
      @Value("${vtm.url.service.userMgmt.getProfile}")
      private String userMgmtServiceURL;
  </code></pre>
  
  <p><strong>This is my InjectConfiguration</strong></p>
  
  <pre><code>package com.att.vtm.changerecord.conditionalinjection;
  
  import javax.sql.DataSource;
  
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Conditional;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  
  import com.att.vtm.changerecord.services.approverreviewer.wrapper.ApproverReviewerDaoAdapter;
  import com.att.vtm.changerecord.services.changerecord.wrapper.ChangeRecordDaoAdapter;
  import com.att.vtm.changerecord.services.changerecord.wrapper.ChangeRecordMetadataDaoAdapter;
  import com.att.vtm.changerecord.services.changerecord.wrapper.ChangeRecordReportAdapter;
  import com.att.vtm.changerecord.services.changerecord.wrapper.ChangeRecordTaskDaoAdapter;
  import com.att.vtm.changerecord.services.changerecord.wrapper.NotificationHelper;
  import com.att.vtm.crjob.services.crjob.wrapper.CRJobDaoAdapter;
  import com.att.vtm.crjob.services.crjob.wrapper.DeviceTestJobsDaoAdapter;
  
  
  @Configuration
  public class InjectionConfiguration {
  
      @Value("${db.driver}")
      private String DB_DRIVER;
  
      @Value("${db.password}")
      private String DB_PASSWORD;
  
      @Value("${db.url}")
      private String DB_URL;
  
      @Value("${db.username}")
      private String DB_USERNAME;
  
  
    @Bean
    @Conditional(DaoCondition.class)
    public ChangeRecordDaoAdapter injectChangeRecordDaoAdapter () throws Exception{
        System.out.println("***INJECTING ChangeRecordDaoAdapter...****");
        return new ChangeRecordDaoAdapter();
    }
  
    @Bean
    @Conditional(DaoCondition.class)
    public ChangeRecordMetadataDaoAdapter injectChangeRecordMetadataDaoAdapter () throws Exception{
        System.out.println("***INJECTING ChangeRecordMetadataDaoAdapter...****");
        return new ChangeRecordMetadataDaoAdapter();
    }
  
    @Bean
    @Conditional(DaoCondition.class)
    public NotificationHelper injectNotificationHelper () throws Exception{
        System.out.println("***INJECTING NotificationHelper...****");
        return new NotificationHelper();
    }
  
    @Bean
    @Conditional(DaoCondition.class)
    public ChangeRecordTaskDaoAdapter injectChangeRecordTaskDaoAdapter () throws Exception{
        System.out.println("***INJECTING ChangeRecordTaskDaoAdapter...****");
        return new ChangeRecordTaskDaoAdapter();
    }
  
    @Bean
    @Conditional(DaoCondition.class)
    public CRJobDaoAdapter injectCRJobDaoAdapter () throws Exception{
        System.out.println("***INJECTING CRJob DAO ADAPTER...****");
        return new CRJobDaoAdapter();
    }
  
  
    @Bean
    @Conditional(DaoCondition.class)
    public DeviceTestJobsDaoAdapter injectDeviceTestJobsDaoAdapter () throws Exception{
        System.out.println("***INJECTING DeviceTestJobs DAO ADAPTER...****");
        return new DeviceTestJobsDaoAdapter();
    }
  
    @Bean
    @Conditional(DaoCondition.class)
    public ChangeRecordReportAdapter injectChangeRecordReportAdapter() throws Exception{
        System.out.println("***....INJECTING DeviceTestJobs DAO ADAPTER.......****");
        return new ChangeRecordReportAdapter();
    }
  
    @Bean
    @Conditional(DaoCondition.class)
    public ApproverReviewerDaoAdapter injectApproverReviewerDaoAdapter() throws Exception{
        System.out.println("***....INJECTING ApproverReviewerDaoAdapter.......****");
        return new ApproverReviewerDaoAdapter();
    }
  
    @Bean
    public JdbcTemplate jdbcTemplate()
    {
        System.out.println("***INJECTING get jdbcTemplate ...****");
        return new JdbcTemplate(dataSource());
    }
  
    @Bean
    public DataSource dataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      System.out.println("***INJECTING dataSource DAO ADAPTER...****");
      System.out.println(" DriverName :"+DB_DRIVER);
      System.out.println(" DB_URL :"+DB_URL);
      System.out.println(" DB_USERNAME :"+DB_USERNAME);
      System.out.println(" DB_PASSWORD :"+DB_PASSWORD);
  
      dataSource.setDriverClassName(DB_DRIVER);
      dataSource.setUrl(DB_URL);
      dataSource.setUsername(DB_USERNAME);
      dataSource.setPassword(DB_PASSWORD);
      return dataSource;
    }
  
   }
  </code></pre>
  
  <p><strong>This is My VTMRefUpdateRepository</strong></p>
  
  <pre><code>package com.att.vtm.changerecord.dao;
  
  import java.util.List;
  
  import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.data.mongodb.repository.Query;
  
  import com.att.vtm.changerecord.dto.VTMRefUserUpdateHistory;
  
  public interface VTMRefUserUpdateHistoryRepository extends MongoRepository&lt;VTMRefUserUpdateHistory, String&gt; {
      // @Query("select * from VTMRefUserUpdateHistory a where v.ticketNo = ?1")
      @Query("{ 'ticketNo' : ?0 }")
      public List&lt;VTMRefUserUpdateHistory&gt; findByTicketNo(String ticketNo);
  
      // @Query("select * from VTMRefUserUpdateHistory a where v.pRNumber = ?1")
      @Query("{ 'pRNumber' : ?0 }")
      public List&lt;VTMRefUserUpdateHistory&gt; findByPRNumber(String pRNumber);
  
      // @Query("select * from VTMRefUserUpdateHistory a where v.cRNumber = ?1")
      @Query("{ 'cRNumber' : ?0 }")
      public List&lt;VTMRefUserUpdateHistory&gt; findByCRNumber(String cRNumber);
  
  
      public List&lt;VTMRefUserUpdateHistory&gt; findByCRNumberAndFieldNameAndNewValue(String cRNumber, String fieldName, String newValue);
  }
  </code></pre>