自动空指针异常

时间:2017-07-21 07:30:50

标签: spring nullpointerexception autowired

我有一个过滤器来保存对DB的请求。但是我在autowired字段上得到NullPointerException:inboundRequestLogStore。

我已经尝试了Using some beans in Filter bean class? (I've added SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext()); to the filter init method and defined a bean in my root-context.xml )的建议,现在它适用于RequestLogFilter类。但是,在InboundRequestLogStore类中,我有另一个自动装配的logService来将我的实体保存到DB,现在我得到了NullPointerEception。为什么我有NPE以及如何解决它?我在项目的其他部分有相同的服务/组件,他们自动服务服务没有任何问题。我只是使用过滤器而遇到麻烦。

@Component
public class RequestLogFilter implements Filter {

    @Autowired
    private InboundRequestLogStore inboundRequestLogStore;

    public RequestLogFilter() {

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
         SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext());
    }


    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest requestToCache = new ContentCachingRequestWrapper((HttpServletRequest) request);
         HttpServletResponseCopier responseCopier = new HttpServletResponseCopier((HttpServletResponse) response);
        chain.doFilter(request, responseCopier);
        responseCopier.flushBuffer();
        if (request.getAttribute(InboundRequestAspect.INBOUND_LOG_MARKER) != null) {

            InboundRequestLogRecord logRecord = new InboundRequestLogRecord();

                setters....

            inboundRequestLogStore.add(logRecord);
        }
    }
}

-

@Component("inboundRequestLogStore")
public class InboundRequestLogStore implements InitializingBean {

    private ConcurrentLinkedQueue<InboundRequestLogRecord> queue = new ConcurrentLinkedQueue<>();

    @Autowired
    private LogService logService;

    @Override
    public void afterPropertiesSet() throws Exception {
        reseterPool = new ScheduledThreadPoolExecutor(1);
        reseterPool.scheduleWithFixedDelay(new LogRequestSaveQueueTask(), startupDelay, sleepTimeout, TimeUnit.SECONDS);
    }   

    public InboundRequestLogStore() {

    }

    public void add(InboundRequestLogRecord logRequest) {
        queue.add(logRequest);
    }

    private class LogRequestSaveQueueTask implements Runnable {
        @Override
        public void run() {
            try {
                if (queue.size() > 0) {
                    List<InboundRequestLogRecord> logRequestList = new ArrayList<>();
                    InboundRequestLogRecord obj;
                    while((obj = queue.poll()) != null) {
                        logRequestList.add(obj);
                    }
                    if (!logRequestList.isEmpty()) {
                        logService.save(logRequestList);
                    }
                }
            } catch(Exception e) {
                logger.error(e.getMessage(), e);
            }
            }
    }
}

栈跟踪

java.lang.NullPointerException: null
    at com.my.project.services.log.InboundRequestLogStore$LogRequestSaveQueueTask.run(InboundRequestLogStore.java:71) [classes/:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_131]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [?:1.8.0_131]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_131]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [?:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

-

    <context:component-scan base-package="com.my.project" />

-

    <bean id="inboundRequestLogStore" class="com.my.project.services.log.InboundRequestLogStore" />

如果使用DelegatingFilterProxy,那么我得到

SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception
java.lang.NullPointerException
    at com.my.project.config.filter.RequestLogFilter.doFilter(RequestLogFilter.java:121)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

的web.xml

<filter>
        <filter-name>requestLogFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>requestLogFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

根context.xml中

<bean id="inboundRequestLogStore" class="com.my.project.services.log.InboundRequestLogStore" />   
<bean id="requestLogFilter" class="com.my.project.config.filter.RequestLogFilter" />

组件(标有bean&#39; id)

@Component("inboundRequestLogStore")
public class InboundRequestLogStore implements InitializingBean {

-

@Component("requestLogFilter")
public class RequestLogFilter implements Filter {

RequestLogFilter上的@Configurable无法正常工作。

结果

ikettu ,谢谢!

经过一些尝试,我已经实现了自动装配的字段不可为空。 我已将bean添加到root-context.xml

<bean id="requestLogFilter" class="com.my.project.config.filter.RequestLogFilter">
    <constructor-arg ref="inboundRequestLogStore"/>
</bean>

<bean id="inboundRequestLogStore" class="com.my.project.service.InboundRequestLogStore">
    <constructor-arg ref="logService"/>
</bean> 

<bean id="logService" class="com.my.project.service.LogService"> 
    <constructor-arg ref="entityManagerFactory"/> 
</bean>

在课程中,我创建了带有这样的参数的构造函数

private LogServic logService;

public InboundRequestLogStore(LogServic logService) {
    this.logService = logService;
} ....

在LogService中

private EntityManagerFactory emf;

public void setEntityManagerFactory(EntityManagerFactory emf) {
    this.emf = emf;
}

但是当我试图将我的实体保存到数据库时,没有发生任何事情。如果我试图刷新一批插入并释放内存

entityManager.flush();
entityManager.clear();

然后NotOpenedTransaction(像这样,我无法想到)异常已经发生

我厌倦了所有人,并创造了一个拦截器。所有的作品都很有趣。如果有人能解释过滤器的问题,请发表评论。

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<!--    <filter> -->
<!--        <filter-name>requestLogFilter</filter-name> -->
<!--        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> -->
<!--        if set this parameter to true then filter init & destroy methods will be called  -->
<!--        <init-param> -->
<!--            <param-name>targetFilterLifecycle</param-name> -->
<!--            <param-value>true</param-value> -->
<!--        </init-param> -->
<!--    </filter> -->
<!--    <filter-mapping> -->
<!--        <filter-name>requestLogFilter</filter-name> -->
<!--        <url-pattern>/*</url-pattern> -->
<!--    </filter-mapping> -->

</web-app>

1 个答案:

答案 0 :(得分:0)

很可能你现在将servlet过滤器初始化为普通的java类而不是初始化的bean。

请使用DelegatingFilterProxy 将春豆作为serlvet过滤器

检查文章https://dzone.com/articles/what-does-spring-delegatingfilterproxy-do