简体   繁体   English

Servlet Spring中的自动装配注释

[英]Autowired annotation in Servlet Spring

I have problem (when i'm calling method from another component class) with NullPointerException like this: 我有这样的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)

My spring version is 3.2 and it's my servlet class injection: 我的春季版本是3.2,这是我的servlet类注入:

@Component
public class FileUploadServlet implements HttpRequestHandler {

    @Autowired
    private MessagesPersistence messagesPersistence;

Next, my applicationContext.xml : 接下来,我的applicationContext.xml

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

    </bean>

spring-servlet.xml : spring-servlet.xml:

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

and entire web.xml : 和整个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>

I tried to try use different settings (without @Component annatotion in servlet, with @Component("UploadServlet") , without config in xml files, and more..) Please help! 我尝试尝试使用其他设置(在servlet中没有@Component annatotion,在@xml中没有@Component @Component("UploadServlet") ,在xml文件中没有配置,等等。)请帮助!

======================EDIT==================================== =====================编辑============================ =========

So.. when I removed <import resource= from spring-servlet.xml autowired in another component in my project doesn't work. 所以..当我<import resource= from spring-servlet.xml删除<import resource= from spring-servlet.xml自动连接到项目中的另一个组件中的操作无效。

My uprgade version of web.xml is: 我的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> 

But it still doesn't work with another exception: 但是它仍然不能与另一个例外一起使用:

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)

Your UploadServlet should be defined in your root web application context (the one loaded up by ContextLoaderListener ): 应该在根Web应用程序上下文(由ContextLoaderListener加载的根上下文)中定义UploadServlet

So the changes should be, add the bean to your applicationContext.xml file, which you have already done. 因此更改应该是将Bean添加到您已经完成的applicationContext.xml文件中。 Remove <import resource.. from your servlet-context.xml file Load up applicationContext.xml instead through ContextLoaderListener , NOT through <import resource... , so in your web.xml: servlet-context.xml文件中删除<import resource..而不是通过ContextLoaderListener而不是通过<import resource... ,加载applicationContext.xml,因此在您的web.xml中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM