我正NullPointerException试着去尝试@Autowire一下@Service:
public class PagePopulationInterceptor extends HandlerInterceptorAdapter {
private @Autowired MyService service;
public @Override void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) {
service.savePageView(IPUtils.toLong(request.getRemoteAddr()), request.getRequestURI(), request.getHeader("User-Agent"));
mav.addObject("counter", service.getCounter());
}
}
@Configuration
@ComponentScan(basePackages = "com.mycompany", excludeFilters = { @ComponentScan.Filter(Configuration.class) })
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
@EnableWebMvc
public class MyConfig extends WebMvcConfigurerAdapter {
public @Override void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PagePopulationInterceptor());
}
}
@Service
@Transactional
public class MyService {
private @Autowired PageViewDao pageViewDao;
public class Counter {
private long …Run Code Online (Sandbox Code Playgroud)