我现在用Spring MVC 的拦截器,准备拦截登录的用户信息等内容,现在在拦截器中和controller中注入用户信息管理类时候报错,先贴异常:
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'HomeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hp.ott2.session.UserSessionInfoManagerInterface com.hp.ott2.web.HomeController.mgr; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.hp.ott2.session.UserSessionInfoManagerInterface] 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)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hp.ott2.session.UserSessionInfoManagerInterface com.hp.ott2.web.HomeController.mgr; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.hp.ott2.session.UserSessionInfoManagerInterface] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
... 76 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.hp.ott2.session.UserSessionInfoManagerInterface] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 78 more

解决方案 »

  1.   

    我的配置文件:
    <mvc:interceptors>
         <bean class="com.hp.ott2.web.interceptor.SiteMinderInterceptor" /> 
    </mvc:interceptors>

    <bean id="userSessionInfo" class="com.hp.ott2.session.UserSessionInfo">
    <aop:scoped-proxy/>
    </bean>

    <bean id="userSessionInfoManager" class="com.hp.ott2.session.UserSessionInfoManager">
    <property name="userInfo" ref="userSessionInfo" />
    </bean>
      

  2.   

    我的拦截器;
    public class SiteMinderInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(SiteMinderInterceptor.class);

    @Autowired
    private UserSessionInfoManager mgr;

    //before the actual handler will be executed
    @SuppressWarnings("unchecked")
    public boolean preHandle(HttpServletRequest request, 
    HttpServletResponse response, Object handler)
        throws Exception {

    logger.info("sm called: "+ request.getHeader("ntuserdomainid"));
    logger.info("sm called: "+ request.getHeader("sm_authorized"));
    logger.info("sm called: "+ request.getHeader("sm_authentic"));
    logger.info("sm called: "+ request.getHeader("sn"));
    logger.info("sm called: "+ request.getHeader("givenname"));
    logger.info("sm called: "+ request.getHeader("uid"));
    logger.info("sm called: "+ request.getHeader("employeename"));

    // for now just return true and allow controller to be called. 
    // note: if false is returned the execution chain is stopped and controller will
    //       not be called. A redirect is possible by calling:
    //       response.sendRedirect("<insert_page_name_here>");
    //       return false

    // check if user info is already stored:
    if(mgr.getUserInfo().getEmployee_no() == null) {
    mgr.getUserInfo().setEmployee_no(request.getHeader("employeename"));
    mgr.getUserInfo().setNtuserdomainid(request.getHeader("ntuserdomainid"));
    mgr.getUserInfo().setSm_authentic((request.getHeader("sm_authentic")!=null ? true:false));
    mgr.getUserInfo().setSm_authorized((request.getHeader("sm_authorized")!=null ? true:false));
    mgr.getUserInfo().setSn(request.getHeader("sn"));
    mgr.getUserInfo().setUid(request.getHeader("uid"));
    mgr.getUserInfo().setGiven_name(request.getHeader("givenname"));
    }

    //for now just allow pass-through to controllers
    return true;
    }
    }
      

  3.   

    我的用户信息管理类:
    public class UserSessionInfoManager { private UserSessionInfo userInfo; public UserSessionInfo getUserInfo() {
    return userInfo;
    } public void setUserInfo(UserSessionInfo userInfo) {
    this.userInfo = userInfo;
    }
    }
      

  4.   

     Could not autowire field: private com.hp.ott2.session.UserSessionInfoManagerInterface com.hp.ott2.web.HomeController.mgr; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.hp.ott2.session.UserSessionInfoManagerInterface] 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)}
    信息很明确,autowire时候出问题了,检查一下类里面的属性名称以及配置里面的bean是否对应的上