无法在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 total;

private long today;

private long yesterday;

private long now;

// Getters and setters

}

public void savePageView(long ip, String visitPage, String userAgent) {

PageView obj = new PageView();

obj.setVisitDate(new Date());

obj.setUserAgent(userAgent);

obj.setPage(visitPage);

obj.setIp(ip);

pageViewDao.saveOrUpdate(obj);

}

public Counter getCounter() {

Counter ret = new Counter();

// populate Counter members

return ret;

}

}

编辑:这是Stacktrace:

java.lang.NullPointerException

com.mycompany.util.PagePopulationInterceptor.postHandle(PagePopulationInterceptor.java:22)

org.springframework.web.servlet.HandlerExecutionChain.applyPostHandle(HandlerExecutionChain.java:149)

org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:934)

org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)

org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)

javax.servlet.http.HttpServlet.service(HttpServlet.java:621)

org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)

javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

回答:

那是因为Spring不在管理您的PagePopulationInterceptor实例。您在以下代码中自行创建

public @Override void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(new PagePopulationInterceptor());

}

更改为

@Bean

public PagePopulationInterceptor pagePopulationInterceptor() {

return new PagePopulationInterceptor();

}

public @Override void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(pagePopulationInterceptor());

}

这样,由于PagePopulationInterceptor实例是从@Bean方法生成的,因此Spring将管理实例的生命周期。Spring将扫描它以寻找@Autowired目标并注入它们。

假设它PagePopulationInterceptor在要@ComponentScan编辑的包中。

以上是 无法在HandlerInterceptorAdapter中自动装配服务 的全部内容, 来源链接: utcz.com/qa/427255.html

回到顶部