title: error using Autowired in AuthenticationProvider
body: <p>I'm trying to use an autowired object in my authenticationProvider but get an error. Without the use of the service object in the class there is not a problem, but ofcours I need the service object.</p>
  
  <p>the customAuthenticationprovider class:</p>
  
  <pre><code>@Component
  public class CustomAuthenticationProvider implements AuthenticationProvider {
  @Autowired
  private Service service;
  
  @Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  
      String name = authentication.getName();
      String password = authentication.getCredentials().toString();
  
      if (service.LoginCorrect(name, password)) {
  
          // use the credentials
          // and authenticate against the third-party system
          List&lt;GrantedAuthority&gt; grantedAuths = new ArrayList&lt;&gt;();
          grantedAuths.add(new SimpleGrantedAuthority(service.getPerson(name).getRole().role()));
          return new UsernamePasswordAuthenticationToken(
                  name, password, grantedAuths);
      } else {
          return null;
      }
  
  }
  
  @Override
  public boolean supports(Class&lt;?&gt; authentication) {
      return authentication.equals(UsernamePasswordAuthenticationToken.class);
  }}
  </code></pre>
  
  <p>Class where is use the custom provider:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @ComponentScan("view.controller")
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  private CustomAuthenticationProvider authenticationProvider;
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
  
      http
              .authorizeRequests()
              .antMatchers("/register/**").permitAll()
              .anyRequest().authenticated()
              .and()
              .formLogin()
              .loginPage("/login").permitAll();
  }
  
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.authenticationProvider(authenticationProvider);
  }}
  </code></pre>
  
  <p>The Creation of the Bean:</p>
  
  <pre><code>@Configuration
  class ApplicationConfig extends WebMvcConfigurerAdapter {
  
  @Bean(destroyMethod = "close")
  public Service service() {
      return new Service("JPADatabase");
  }
  }
  </code></pre>
  
  <p>The Initialize for the security: </p>
  
  <pre><code>public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[]{ApplicationConfig.class, WebSecurityConfig.class};
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class[]{DispatcherServletConfig.class};
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[]{"/"};
      }
  
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>Warning:   Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'authenticationProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in file [D:\Users\neuts\Documents\NetBeansProjects\project-ip-vincentneuts\spring-mvc-kopie\target\r0621919\WEB-INF\classes\view\controller\CustomAuthenticationProvider.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [view.controller.CustomAuthenticationProvider]: Constructor threw exception; nested exception is java.lang.RuntimeException: Uncompilable source code - cannot find symbol
      symbol:   class Autowired
      location: class view.controller.CustomAuthenticationProvider
    Severe:   Context initialization failed
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'authenticationProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in file [D:\Users\neuts\Documents\NetBeansProjects\project-ip-vincentneuts\spring-mvc-kopie\target\r0621919\WEB-INF\classes\view\controller\CustomAuthenticationProvider.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [view.controller.CustomAuthenticationProvider]: Constructor threw exception; nested exception is java.lang.RuntimeException: Uncompilable source code - cannot find symbol
      symbol:   class Autowired
      location: class view.controller.CustomAuthenticationProvider</p>
  </blockquote>
  
  <p>EDIT: IT only works on the first run. (after restarting my glassfish server)</p>