在像 Servlet 或 Filter 这样的单例 bean 中注入一个带有 scope="request" bean 的 aop:scoped-proxy 是线程安全的吗?

Is thread safe to inject an aop:scoped-proxy with scope="request" bean in a singleton bean like a Servlet or a Filter?

我有一个 javax.servlet.Filter(因此站点的所有用户共享的单例),其中注入了一个 aop:scoped-proxy with scope="request"。

过滤代码如下:

public class RequestLoggerFilter extends GenericFilterBean {

@Inject
private RequestMonitoringDetail monitoringDetail;    

这里是代理 bean 配置:

<bean id="requestMonitoringDetail" class="com.logging.data.RequestMonitoringDetail" scope="request">
  <aop:scoped-proxy />
</bean>

RequestMonitoringDetail bean 的范围是请求,因此将为每个请求创建一个新实例,并由过滤器中注入的代理使用。 我以为它是线程安全的。

问题是在代理上调用方法时不时出现 NullPointerException:

java.lang.NullPointerException
    at org.apache.catalina.connector.Request.notifyAttributeAssigned(Request.java:1555)
    at org.apache.catalina.connector.Request.setAttribute(Request.java:1541)
    at org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:540)
    at org.springframework.web.context.request.ServletRequestAttributes.setAttribute(ServletRequestAttributes.java:124)
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:44)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:34)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:665)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:616)
    at com.logging.data.RequestMonitoringDetail$$EnhancerBySpringCGLIB$c595aed.getShortLogDetailSuffixesForMonitoring(<generated>)
    at ...

怎么会这样?

我终于找到了原因:tomcat 正在关机。 就在错误之前,有一个关闭的信号:

12-Jan-2021 18:18:11.550 INFOS [RMI TCP Connection(181)-10.109.0.23] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio2-0.0.0.0-8009"]

还不知道它从哪里来的,但它似乎是之后错误的原因。