在拦截器中注入 service的方法

1)在实现类或xml配置文件中声明 service实现类的别名,我是在实现类上直接使用注解声明的

在拦截器中注入 service的方法_第1张图片

2)springmvc.xml配置文件中配置拦截器实现类

在拦截器中注入 service的方法_第2张图片

3)在拦截器中使用以下代码注入

public class TestIntercepter implements HandlerInterceptor {

 

@Autowired

private IweixinUserService iweixinUserService; //service接口

public boolean preHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o) throws Exception {

XmlWebApplicationContext cxt =(XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());

if(cxt != null && cxt.getBean("weixinUserService") != null && iweixinUserService == null) {

iweixinUserService = (IweixinUserService) cxt.getBean("weixinUserService"); // weixinUserService 是实现类的别名

}

System.out.println("wxuser:"+iweixinUserService.selectByPrimaryKey("111111")); //这里调用service方法查询用户数据

return false;

}

public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

}

public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

}

}

 

你可能感兴趣的:(java开发笔记,Java基础)