springboot中自定义拦截器无法通过注入方式获取需要使用的Dao

通过WebApplicationContextUtils内部的getRequiredWebApplicationContext方法,该方法需要传入一个实体的类型,以及一个HttpServetRequest请求对象,获取到BeanFactory(实体工厂类),从而通过工厂实体的getBean方法就可以拿到SpringDataJPA为我们管理的LoggerJPA持久化数据接口实例。

private T getDAO(Class clazz,HttpServletRequest request){
        BeanFactory factory= WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());

        return factory.getBean(clazz);


    }

你可能感兴趣的:(springboot中自定义拦截器无法通过注入方式获取需要使用的Dao)