相关疑难解决方法(0)

无法在HandlerInterceptorAdapter中自动装配服务

我正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)

java spring spring-mvc

41
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

spring ×1

spring-mvc ×1