问题的权利:我的@Autowired
数据和我在@PostConstruct
方法中设置的任何内容都不会被保留。在这种情况下,我可能会误解bean的生命周期。
这是我的班级:
@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);
}
}
我用评论标记了两个问题区域。