使用“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等都可使用了。
———————
作者:梦里藍天
来源:CSDN
原文:https://blog.csdn.net/ren365880/article/details/78037414
版权声明:本文为博主原创文章,转载请附上博文链接!
- 本文固定链接: https://www.maxwoods.net/archives/1712
- 转载请注明: 第五维 于 第五维 发表