在自定义realm的时候注入的时候就会报错,去掉注解@Autowired 运行就会没有问题。错误的原因应该在于注解无法注入
@Autowired
private UserService userService;
// 设置realm的名称
@Override
public void setName(String name) {
super.setName("customRealm");
}
这是eclipse报的异常情况。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in
class path resource [spring-shiro/spring-shiro.xml]:
Cannot resolve reference to bean 'securityManager'
while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'securityManager' defined in class path resource [spring-shiro/spring-shiro.xml]:
Cannot resolve reference to bean 'myRealm' while setting bean property 'realm';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myRealm':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.englishload.service.UserService com.englishload.realm.MyRealm.userService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
[com.englishload.service.UserService]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方案:
这是改变后的代码,将SpringMVC的配置文件在
<context-param>中加载。
<!-- spring mybatis配置 和shiro-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis/spring-mybatis.xml,
classpath:spring-shiro/spring-shiro.xml,
classpath:/springmvc-servlet/springmvc-servlet.xml
</param-value>
</context-param>
<!-- springmvc的配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springmvc-servlet/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- shiro的filter -->
<!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spring容器中的bean和filter关联起来 -->