问题产生原因:加载Realm时其他Spring配置文件(xml)尚未加载,导致注入失败。
解决方法:编写一个设置类把注入工作提前完成。
package com.xkt.shiro import org.apache.shiro.realm.Realm; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Configuration; /** * 把securityManager和userRealm装配到ApplicationContext */ @Configuration public class ShiroConfig implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext ac) throws BeansException { Realm userRealm = (Realm) ac.getBean("userRealm"); DefaultWebSecurityManager defaultWebSecurityManager = (DefaultWebSecurityManager) ac.getBean("securityManager"); defaultWebSecurityManager.setRealm(userRealm); } }