title: Autowired in CustomInterceptor getting null(Spring Boot)
body: <p>I want to make a check in database when i receive a request. So i did a Interceptor like below,</p>
  
  <p>CustomInterceptor.java</p>
  
  <pre><code>@Component
  public class CustomInterceptor extends HandlerInterceptorAdapter {
  
  @Autowired
  private DatabaseService databaseService;
  
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      //Set Request Attribute(TODO)
      LogService.info(this.getClass().getName(), "New Request URI is:" + request.getRequestURI());
      return true;
  }
  
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      String authToken = request.getHeader("AuthToken");
          boolean isValidRequest = databaseService.checkIfTokenIsValid(authToken);
      }
  }
  </code></pre>
  
  <p>Application.class:</p>
  
  <pre><code>@SpringBootApplication
  public class Application extends SpringBootServletInitializer {
  
  //    protected Properties props = new Properties();
  //
  //    public Application() {
  //        props.setProperty("error.path", "/error");
  ////        props.setProperty("error.whitelabel.enabled", "false");
  ////        props.setProperty("org.springframework.web", "DEBUG");
  //    }
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  //        application.properties(props);
      return application.sources(Application.class);
  }
  
  @Override
  public void onStartup(final ServletContext servletContext) throws ServletException {
      LogService.info(Application.class.getName(), "Loading Service...");
      super.onStartup(servletContext);
      LogService.info(Application.class.getName(), "Service Started");
  }
  
  public static void main(String[] args) {
      ApplicationContext context = SpringApplication.run(Application.class, args);
  }
  </code></pre>
  
  <p>DatabasService.java</p>
  
  <pre><code>@Service
  public class DatabaseService {
  
  
  @Autowired
  private ApplicationProperties properties;
  
  private final JdbcTemplate defaultJdbcTemplate;
  
  @Autowired
  public DatabaseService(
          @Qualifier("dataSource") DataSource dataSource) {
      defaultJdbcTemplate = new JdbcTemplate(dataSource);
  }
  
  public boolean checkIfTokenIsValid() {
      //Perform Check
   }
  }
  </code></pre>
  
  <p>CustomWebConfiguration.java</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class CustomWebConfiguration extends WebMvcConfigurerAdapter {
  
  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
      "classpath:/META-INF/resources/", "classpath:/resources/",
      "classpath:/static/", "classpath:/public/"};
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      if (!registry.hasMappingForPattern("/**")) {
          registry.addResourceHandler("/**").addResourceLocations(
                  CLASSPATH_RESOURCE_LOCATIONS);
      }
  }
  
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(new CustomInterceptor())
              .addPathPatterns("/**");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But i get NullPointer At: <code>boolean isValidRequest = databaseService.checkIfTokenIsValid(authToken);</code></p>
  
  <p>What is wrong here, why cannot spring Autowire the Databaseservice in Interceptor?</p>
  
  <p>Note: Autowire works fine everywhere else, but not in the interceptor.</p>
  
  <p><strong>Solution (Thanks to M. Deinum)</strong>
  Change the CustomWebConfiguration.java like below;</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class CustomWebConfiguration extends WebMvcConfigurerAdapter {
  
  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
      "classpath:/META-INF/resources/", "classpath:/resources/",
      "classpath:/static/", "classpath:/public/"};
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      if (!registry.hasMappingForPattern("/**")) {
          registry.addResourceHandler("/**").addResourceLocations(
                  CLASSPATH_RESOURCE_LOCATIONS);
       }
      }
  
  @Bean
  public CustomInterceptor customInterceptor() {
      return new CustomInterceptor();
  }
  
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(customInterceptor())
              .addPathPatterns("/**");
   }
  }
  </code></pre>