title: @Autowire is not working in Spring security Custom Authentication provider
body: <p>We have Spring MVC application. We are trying to integrate the Spring security in it. </p>
  
  <p>We have written our custom authentication provider which will do the work of authentication. </p>
  
  <p>Below is the code for my custom authentication provider. </p>
  
  <pre><code>    public class CustomAuthenticationProvider extends DaoAuthenticationProvider {
  
      @Autowired
      private AuthenticationService authenticationService;
  
      @Override
      public Authentication authenticate(Authentication authentication) {
  
          CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;
  
          String username = String.valueOf(auth.getPrincipal());
          String password = String.valueOf(auth.getCredentials());
  
          try {
  
              Users user = new User();
              user.setUsername(username);
              user.setPassword(PasswordUtil.encrypt(password));
  
              user = authenticationService.validateLogin(user);
  
              return auth;
          } catch (Exception e) {
              throw new BadCredentialsException("Username/Password does not match for " + username);
          }
      }
  
      @Override
      public boolean supports(Class&lt;? extends Object&gt; authentication) {
          return (CustomAuthenticationToken.class.isAssignableFrom(authentication));
  
      }
  }
  </code></pre>
  
  <p>Here i am getting NullpointerException on the following line </p>
  
  <pre><code>user = authenticationService.validateLogin(user);
  </code></pre>
  
  <p>The authenticationService is not getting autowired in the custom authentication provider. While the same service authenticationService is autowired in the same way in my MVC controller. </p>
  
  <p>Is this because authentication provider is a Spring security component?</p>
  
  <p>Below is a my web.xml</p>
  
  <pre><code>    &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          /WEB-INF/spring/myApp-security.xml
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;myApp&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/myApp-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;myApp&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p><strong>Edit 1 :-</strong> </p>
  
  <p>I have added the following lines in my spring security configuration file.</p>
  
  <pre><code>&lt;beans:bean id="customAuthenticationProvider" class="com.myApp.security.provider.CustomAuthenticationProvider"&gt;
      &lt;beans:property name="userDetailsService" ref="userDetailsService"/&gt;   
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p>Please help how to autowire my service classes in the Spring security components?</p>