title: Spring Security config autowiring custom UserDetailsService bean
body: <p>I recently came back to a Spring project I'd been working on and I've run into issues when starting up the app. This question is probably a duplicate, but I haven't been able to find an answer.</p>
  
  <p>Here's a snippet from my original SecurityConfig.java:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired private UserService userService;
  
      /**
       * Global security config to set the user details service etc.
       * @param auth authentication manager
       * @throws Exception
       */
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth
                  .userDetailsService(userService)
                  .passwordEncoder(passwordEncoder());
      }
  </code></pre>
  
  <p>The UserService object implements UserDetailsService. This gave the error on startup:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.clubmate.web.service.UserService com.clubmate.web.config.SecurityConfig.userService; nested exception is java.lang.IllegalArgumentException: Can not set com.clubmate.web.service.UserService field com.clubmate.web.config.SecurityConfig.userService to com.sun.proxy.$Proxy62
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      ... 58 more
  Caused by: java.lang.IllegalArgumentException: Can not set com.clubmate.web.service.UserService field com.clubmate.web.config.SecurityConfig.userService to com.sun.proxy.$Proxy62
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
      at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
      at java.lang.reflect.Field.set(Field.java:764)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
      ... 60 more
  </code></pre>
  
  <p>From my reading, this is because I'm autowiring a concrete class instead of the UserDetailsService interface, but this is strange to me, because when I worked on this project before, autowiring the concrete class worked fine.</p>
  
  <p>I gave in and just changed it to autowire the UserDetailsService interface in SecurityConfig.java. </p>
  
  <p>I'm not sure whether this is related or not, but now on startup I get the below error, for any of my other bean objects (<code>@Service</code>s etc.) that have <code>@Autowire private UserService userService</code>.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.clubmate.web.service.UserService com.clubmate.web.service.PageService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.clubmate.web.service.UserService] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      ... 58 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.clubmate.web.service.UserService] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
      ... 60 more
  </code></pre>
  
  <p>Any help greatly appreciated. This has been driving me nuts for hours.</p>
  
  <p><strong>Update</strong></p>
  
  <p>More info: I have another config class (AppConfig.java) with the following:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan("com.clubmate.web")
  @PropertySource(value = { "classpath:application.properties" })
  @Import({
          SecurityConfig.class,
          CacheConfig.class,
          DatabaseConfig.class,
          CronConfig.class
  })
  public class AppConfig extends WebMvcConfigurerAdapter {
      // ...
  }
  </code></pre>
  
  <p>I also have two app initializer classes, one for MVC which is:</p>
  
  <pre><code>public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      public Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[] {
                  AppConfig.class
          };
      }
  
      @Override
      public Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class[] {
                  StartupHousekeeping.class,
                  ShutdownHousekeeping.class
          };
      }
  
      @Override
      public String[] getServletMappings() {
          return new String[] {
                  "/"
          };
      }
  
  }
  </code></pre>
  
  <p>...and another for Security which is:</p>
  
  <pre><code>public class AppInitializer extends AbstractSecurityWebApplicationInitializer {
  
      private static Logger log = LogManager.getLogger(AppInitializer.class);
  
      @Override
      protected void beforeSpringSecurityFilterChain(ServletContext context) {
          // load multipart filter before other filters are created
          // see: http://docs.spring.io/spring-security/site/docs/4.0.1.RELEASE/reference/htmlsingle/#csrf-multipart
          MultipartFilter multipartFilter = new MultipartFilter();
          multipartFilter.setMultipartResolverBeanName("multipartResolver");
          insertFilters(context, multipartFilter);
      }
  
  }
  </code></pre>
  
  <p>Could these be conflicting somehow?</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>Screenshot of where the exception is being thrown: <a href="http://i.imgur.com/r6AsOob.jpg" rel="nofollow">http://i.imgur.com/r6AsOob.jpg</a></p>
  
  <p>var1 is the SecurityConfig class, var2 is a Proxy object, which should instead be my UserService class.</p>