使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法

先来看问题

这里写图片描述 
这里写图片描述 
这里写图片描述

纠结了几个小时终于找到了问题所在,因为shiro的realm属于Filter,简单说就是初始化realm时,spring还未加载相关业务Bean,那么解决办法就是将springmvc的配置文件加载提前。

解决办法 
打开web.xml文件

这里写图片描述 
这里写图片描述

OK,问题解决!

解决方式2(不建议)

public TenantServiceTypeTask(ServletContextEvent event) {
        super();
        ServletContext sc=event.getServletContext();
        ApplicationContext beans = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        userService = (TenantIdentityService) beans.getBean("userService");
    }

通过手动的形式,从spring的容器中获取相应的bean.

参考:

http://blog.csdn.net/qq_32588349/article/details/51793822

原文地址:https://www.cnblogs.com/chihirotan/p/6420712.html