gpt4 book ai didi

java - 无法在 HandlerInterceptorAdapter 中 Autowiring 服务

转载 作者:IT老高 更新时间:2023-10-28 13:04:35 28 4
gpt4 key购买 nike

我在尝试 @Autowire 我的 @Service 时遇到 NullPointerException:

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());
}

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

这假定 PagePopulationInterceptor 在要被 @ComponentScan 编辑的包中。

关于java - 无法在 HandlerInterceptorAdapter 中 Autowiring 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18218386/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com