我的Controller bean(Spring-MVC)中没有处理自动装配的bean

时间:2010-09-06 11:55:02

标签: java spring spring-mvc controller autowired

我创建一个Controller bean来映射一个专用的URI。

web.xml 文件:

    <!-- Spring MVC Servlet (that will route HTTP requests to BlazeDS) -->
 <servlet>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-main-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- We are only using BlazeDS AMF channels to communicate with FlexClient, so let's map URLs accordingly. -->
 <servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/messagebroker/*</url-pattern>        
 </servlet-mapping>   

        <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-filter-config.xml</param-value>
        </context-param>

        <listener>
               <listener-class>
                org.springframework.web.context.ContextLoaderListener
               </listener-class>
        </listener>

spring-main-config.xml 文件:

 <!-- Import configuration base -->
 <import resource="spring-base-config.xml"/>  

 <!-- Flex integration within Spring. -->
 <flex:message-broker services-config-path="/WEB-INF/flex-services-config.xml">
  <flex:remoting-service />
 </flex:message-broker>

 <!-- Session-scoped bean for User Info -->
 <bean class="com.sharehunter.businessservices.common.UserSessionInfo" scope="session">
  <aop:scoped-proxy proxy-target-class="false"/>
 </bean>    

spring-filter-config.xml 文件:

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
   <value>
   /fbauth=myController
   </value>
  </property>
 </bean>

 <bean id="myController" 
  class="myapp.controller.myController" />

我的bean控制器文件:

package myapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

    public class MyController extends AbstractController{

    @Autowired
    UserSesson userSession;

    @Autowired
    BusinessLogger logger;

 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest request,
   HttpServletResponse response) throws Exception {

  // TO DO - Facebook authentication
  return null;
 }
 }

我的问题:我的Controller bean中的所有Autowired bean已经等于null。我不明白我的配置问题在哪里...

非常感谢你的帮助!

安东尼

2 个答案:

答案 0 :(得分:2)

您定义了两个不同的contextx(spring-filter-config.xmlspring-main-config.xml),但您应该只有一个。 <context:annotation-driven/>仅在spring-main-config.xml中定义,而不在spring-filter-config.xml中定义,因此spring-filter-config.xml中定义的任何bean都不会自动装配。

您在spring-filter-config.xml中定义了控制器,但控制器应与ServiceDispatcherServlet相关联。您需要将spring-filter-config.xml的内容移至spring-main-config.xml,然后移除ContextLoaderListener - 您似乎没有正确使用它。

答案 1 :(得分:0)

你的bean映射在哪里?

在xml文件中提到那么@Autowired默认识别要调用哪个bean类...