title: Autowire doesn't work for custom UserDetailsService in Spring Boot
body: <p>The spring boot apps starts up, tomcat runs, and then it throws an error before finally dying.</p>
  
  <h1>Error</h1>
  
  <p>Running my app gives me this <code>Autowired</code> error:</p>
  
  <pre><code>2016-07-29 02:18:24.737 ERROR 44715 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed
  
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [myapp.MyUserDetailsService] 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)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  </code></pre>
  
  <h1>Details</h1>
  
  <p>My Security configuration:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
          http
          .authorizeRequests()
          .antMatchers("/", "/register").permitAll()
          .anyRequest().authenticated()
          .and()
          .formLogin()
          .loginPage("/")
          .permitAll()
          .and()
          .logout()
          .permitAll();
  }
  
  @Autowired
  private MyUserDetailsService userDetailsService;
  
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService);
  }
  
  }
  </code></pre>
  
  <p>MyUserDetails:</p>
  
  <pre><code>@Service
  @Transactional
  public class MyUserDetailsService implements UserDetailsService {
  
  @Autowired
  private UserRepository userRepository;
  
  
  public MyUserDetailsService() {
          super();
  }
  
  @Override
  public UserDetails loadUserByUsername(String userName)
  throws UsernameNotFoundException {
          User user = userRepository.findByUserName(userName);
          if (user == null) {
                  return null;
          }
          List&lt;GrantedAuthority&gt; auth = AuthorityUtils
                                        .commaSeparatedStringToAuthorityList("ROLE_USER");
          String password = user.getPassword();
          return new org.springframework.security.core.userdetails.User(userName, password,
                                                                        auth);
  }
  
  
  
  }
  </code></pre>
  
  <p>You may find full source code at <a href="http://Github.com/amingilani/spring-boot-tutorial" rel="nofollow">Git Repo</a></p>