SpringBoot intercept拦截器中获取JPA数据库操作实例

来源:互联网 发布:查看阿里云ecs带宽 编辑:程序博客网 时间:2024/06/12 21:45

使用“org.springframework.web.context.support.WebApplicationContextUtils;”类

这个工具类可以通过HttpServletRequest请求对象的上下文(ServetCotext)获取Spring管理的Bean

private <T> T getMapper(Class<T> clazz,HttpServletRequest request)  {    BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());    return factory.getBean(clazz);  }

使用时
SystemLogRepository systemLogRepository = getMapper(SystemLogRepository.class,arg0);
获取了JPA接口实例,。save,findone,findAll等都可使用了。

原创粉丝点击