title: Spring Boot Annotation @Autowired of Service fails
body: <p>I'm trying to use <code>@Autowired</code> annotation for a Service class in Spring Boot application, but it keeps throwing <code>No qualifying bean of type</code> exception. However, if I change the service class to a bean, then it works fine. This is my code:</p>
  
  <pre><code>package com.mypkg.domain;
  @Service
  public class GlobalPropertiesLoader {
  
      @Autowired
      private SampleService sampleService;        
  }
  
  package com.mypkg.service;
  @Service
  public class SampleService{
  
  }
  </code></pre>
  
  <p>And this is my SpringBoot class:</p>
  
  <pre><code>package com.mypkg;
  
  import java.util.Set;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.config.BeanDefinition;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Import;
  
  @SpringBootApplication
  @EnableJpaRepositories
  @Import(RepositoryRestMvcConfiguration.class)
  @EnableTransactionManagement
  public class TrackingService {
      private static final Logger LOGGER = LoggerFactory.getLogger(TrackingService.class);
  
      static AnnotationConfigApplicationContext context;
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(TrackingService.class, args);
          context = new AnnotationConfigApplicationContext();
          context.refresh();
          context.close();
  
      }
  
  }
  </code></pre>
  
  <p>When I try to run this, I get the following exception:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mypkg.service.SampleService] 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>But when I remove the <code>@Service</code> annotation from the SampleService class, and add it as a bean in my AppConfig class as below, it works fine:</p>
  
  <pre><code>@Configuration
  public class AppServiceConfig {
  
      public AppServiceConfig() {
      }
  
      @Bean(name="sampleService")
      public SampleService sampleService(){
          return new SampleService();
      }
  
  }
  </code></pre>
  
  <p>The classes are in different packages. I am not using <code>@ComponentScan</code>. Instead, I'm using <code>@SpringBootApplication</code> which does that automatically. However, I tried with ComponentScan as well but that didn't help.</p>
  
  <p>What am I doing wrong here?</p>