@Autowire在servlet中为null

时间:2014-05-23 22:32:38

标签: java spring spring-mvc servlets

如果我使用@Autowired,这个servlet中的服务为null,使用context.getBean()可以正常工作;此外,使用/ floorOperationWS /的映射没有完成,我必须在web.xml中定义映射。

package com.confloorapp.services.endpoint;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



@WebServlet(value = "/floorOperationWS/")
public class UpdateFloorEventServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());


            FloorService floorService = (DoorService)context.getBean("floorService");

}



}

这里是applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:sws="http://www.springframework.org/schema/web-services"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/web-services 
            http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config />

    <!-- Scans the classpath for annotated components that will be auto-registered 
        as Spring beans. For example @Controller and @Service. Make sure to set the 
        correct base-package -->
    <context:component-scan base-package="com.confloorapp" />
    <context:annotation-config/>

    <!-- Configures the annotation-driven Spring MVC Controller programming 
        model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
    <mvc:annotation-driven />

    <!-- Load Hibernate related configuration -->
    <!-- Here you can also add spring security context, if exist -->
    <import resource="hibernate-context.xml" /> 
    <import resource="spring-security.xml" />

</beans>

1 个答案:

答案 0 :(得分:8)

由于Spring容器不处理servlet,因此自动装配的注释不能立即使用。要解决这个问题,您可以自动装配servlet中的所有Spring bean,如下所示:

public class UpdateFloorEventServlet extends HttpServlet {

    @Autowired
    private FloorService floorService;

    private WebApplicationContext springContext;

    @Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);
        springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
        final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
        beanFactory.autowireBean(this);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

现在,FloorService对象将由Spring正确自动装配,并且没有空值。

当应用程序使用bean时,spring上下文文件必须包含所有bean定义。

另外,init方法本身可以在一个单独的Servlet类中实现,该类将扩展HttpServlet。然后,您可以让所有Servlet扩展该类,因此它们都可以使用init方法,从而避免代码重复。