SpringMvc+MyBatis+Shiro整合,shiro的realm不能注入Bean

来源:互联网 发布:centos快速打开终端 编辑:程序博客网 时间:2024/05/16 08:02

springMvc+Mybatis和shiro整合,shiro的realm引入Autowire加入接口数据,但是一直无法成功注入,提示问题如下:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sxkj.service.inter.UserServerInter] 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注入配置问题,但是controller注入又都正常,此时陷入百思不得解。一项项跟踪,发现原来shiro 自定义realm的认证阶段属于filter,当时的spring bean还没有读取进来。
最后通过配置web.xml文件,把spring mvc的xml提高一点优先级,才最终解决了这个问题。下面是web.xml完整配置:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:spring-mybatis.xml,classpath:dispatcher-servlet.xml,classpath:spring-shiro.xml</param-value>  </context-param>  <servlet>    <servlet-name>dispatcher</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:dispatcher-servlet.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <!-- 设置拦截 !-->  <servlet-mapping>    <servlet-name>dispatcher</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>log4jContextName</param-name>    <param-value>log4jContext</param-value>  </context-param>  <!-- 添加shiro的过滤器代理!-->  <filter>     <filter-name>shiroFilter</filter-name>     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>     <async-supported>true</async-supported>    <init-param>      <param-name>targetFilterLifecycle</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>shiroFilter</filter-name> <!-- 此处必需写成"/*",如果只写"/" ,过滤器没有作用-->    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>
红色为添加的部分,有碰到此问题的同学可以看看。

 

原创粉丝点击