我有这样的问题(当我从另一个组件类调用方法时)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>
弹簧-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>
我试图尝试使用不同的设置(@Component
在 servlet 中没有 annatotion,@Component("UploadServlet")
在 xml 文件中没有配置,等等..)请帮忙!
======================编辑=========================== =========
所以..当我 <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)