Servlet Spring中的自动注释

时间:2013-04-30 12:04:17

标签: java spring-mvc dependency-injection annotations

我有NullPointerException这样的问题(当我从另一个组件类调用方法时):

ERROR [UploadServlet] - Servlet.service() for servlet UploadServlet threw exception
java.lang.NullPointerException
    at pl.crai.servlets.FileUploadServlet.doPost(FileUploadServlet.java:77)
    at pl.crai.servlets.FileUploadServlet.handleRequest(FileUploadServlet.java:286)
    at org.springframework.web.context.support.HttpRequestHandlerServlet.service(HttpRequestHandlerServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

我的春季版本是3.2,这是我的servlet类注入:

@Component
public class FileUploadServlet implements HttpRequestHandler {

    @Autowired
    private MessagesPersistence messagesPersistence;

接下来,我的applicationContext.xml

<bean name="UploadServlet" class="pl.crai.servlets.FileUploadServlet">

    </bean>

spring-servlet.xml:

<mvc:annotation-driven />
    <import resource="applicationContext.xml" />
    <context:component-scan base-package="pl.crai" />

和整个web.xml

<display-name></display-name>

    <listener>
      <listener-class>
        org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping> 
        <filter-name>CharacterEncodingFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping >

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

      <servlet>
        <servlet-name>downloadAttachments</servlet-name>
        <servlet-class>pl.crai.servlets.DownloadAttachmentsSerlvet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>downloadAttachments</servlet-name>
        <url-pattern>/downFile/*</url-pattern>
    </servlet-mapping>

     <servlet>
        <servlet-name>downloadMultipleAttachments</servlet-name>
        <servlet-class>pl.crai.servlets.DownloadMultipleAttachmentsServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>downloadMultipleAttachments</servlet-name>
        <url-pattern>/downMultipleFile/*</url-pattern>
    </servlet-mapping>


     <servlet>
    <display-name>UploadServlet</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>
    org.springframework.web.context.support.HttpRequestHandlerServlet

    </servlet-class>

</servlet>

<servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/uploadFile</url-pattern>
</servlet-mapping>

    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>pl.crai.filters.LoginFilter</filter-class>

    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>

</web-app>

我尝试尝试使用不同的设置(在servlet中没有@Component annatotion,@Component("UploadServlet"),在xml文件中没有配置,等等。) 请帮忙!

====================== EDIT ======================== ============

所以..当我在我的项目中的另一个组件中删除<import resource= from spring-servlet.xml自动装配时不起作用。

我的web.xml的uprgade版本是:

<display-name></display-name>
    <listener>
      <listener-class>
        org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping> 
        <filter-name>CharacterEncodingFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping >

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

      <servlet>
        <servlet-name>downloadAttachments</servlet-name>
        <servlet-class>pl.crai.servlets.DownloadAttachmentsSerlvet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>downloadAttachments</servlet-name>
        <url-pattern>/downFile/*</url-pattern>
    </servlet-mapping>

     <servlet>
        <servlet-name>downloadMultipleAttachments</servlet-name>
        <servlet-class>pl.crai.servlets.DownloadMultipleAttachmentsServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>downloadMultipleAttachments</servlet-name>
        <url-pattern>/downMultipleFile/*</url-pattern>
    </servlet-mapping>


     <servlet>
    <display-name>UploadServlet</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>
    org.springframework.web.context.support.HttpRequestHandlerServlet
           <!-- org.springframework.web.context.support.HttpRequestHandlerServlet --> 
    </servlet-class>

    </servlet>

    <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/uploadFile</url-pattern>
    </servlet-mapping>


    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>pl.crai.filters.LoginFilter</filter-class>

    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>

</web-app> 

但它仍然不适用于另一个例外:

Allocate exception for servlet UploadServlet
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'UploadServlet' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
    at org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:57)
    at javax.servlet.GenericServlet.init(GenericServlet.java:211)

1 个答案:

答案 0 :(得分:-1)

您的UploadServlet应该在您的根网络应用程序上下文中定义(由ContextLoaderListener加载的那个):

因此应该进行更改,将bean添加到您已经完成的applicationContext.xml文件中。 从<import resource..文件中删除servlet-context.xml 通过ContextLoaderListener而不是<import resource...加载applicationContext.xml,所以在你的web.xml中:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>