【问题标题】:Autowired annotation in Servlet SpringServlet Spring 中的自动装配注解
【发布时间】:2013-04-30 12:04:17
【问题描述】:

我有问题(当我从另一个组件类调用方法时)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)

我的 spring 版本是 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 中没有@Componentannatotion,@Component("UploadServlet"),xml 文件中没有配置,等等..) 请帮忙!

=======================编辑======================== =============

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

我的 web.xml 升级版本是:

<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)

【问题讨论】:

  • 您是否使用适当的contextConfigLocation 设置了Spring ContextLoaderListener
  • 什么时候得到异常?您的注射失败还是采取了措施?
  • 操作时出现异常。
  • 你能展示你的整个 web.xml 吗?
  • 好的,来自 web.xml 的所有内容都在第一篇文章中 :)

标签: java spring-mvc dependency-injection annotations


【解决方案1】:

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

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

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

【讨论】:

  • 我必须在第一篇文章中回答
  • 我检查了您的编辑..您似乎已经完成了所需的一切,奇怪的是它仍然无法正常工作。您在启动时没有看到任何异常吗?是否可以在 github 中放置一个精简的项目?
猜你喜欢
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2012-01-19
  • 2011-03-13
相关资源
最近更新 更多