1

Right to the problem: my @Autowired data and anything I set in my @PostConstruct method is not retained. I might be misunderstanding the bean lifecycle in this case.

Here is my class:

@Component
public class HeaderInspector implements HandlerInterceptorAdapter {
  private static final Logger LOGGER = LoggerFactory.getLogger(HeaderInspector.class);

  @Autowired
  private ConfigurationService configuration;

  @Autowired
  private WaffleHeaderService waffleHeader;

  private String logPrefix;

  @PostConstruct
  public void init() {
    logPrefix = configuration.getLogPrefix("WaffleHeader");
  }

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // logs:  "null Intercept"
    LOGGER.trace(logPrefix + " Intercept");

    // !!! NullPointException !!!
    return waffleHeader.accept(request);
  }
}

I marked the two problem areas with comments.

el n00b
  • 1,957
  • 7
  • 37
  • 64
  • 5 cents: http://stackoverflow.com/questions/28022108/is-spring-handlerinterceptoradapter-posthandle-guaranteed – Anton N Jul 25 '16 at 14:31
  • `@Autowired` properties cannot be `null`... They can only be `null` if you are constructing instances yourself (which makes them non-spring managed beans) or if you are using them in a context which doesn't have annotation processing enabled. Generally it is the first of the 2 by not making it a spring managed bean. I guess you have a `WebMvcConfigurerAdapter` overriden the `addInterceptors` method and inside that method do `registry.addInterceptor(new HeaderInspector())`. – M. Deinum Jul 26 '16 at 05:59

0 Answers0