[英]Is thread safe to inject an aop:scoped-proxy with scope=“request” bean in a singleton bean like a Servlet or a Filter?
I have a javax.servlet.Filter (so a singleton shared by all users of the site) in which an aop:scoped-proxy with scope="request" is injected.我有一个 javax.servlet.Filter(因此站点的所有用户共享一个 singleton),其中注入了一个带有 scope="request" 的 aop:scoped-proxy。
Here is the filter code:这是过滤器代码:
public class RequestLoggerFilter extends GenericFilterBean {
@Inject
private RequestMonitoringDetail monitoringDetail;
Here is the proxied bean config:这是代理的bean配置:
<bean id="requestMonitoringDetail" class="com.logging.data.RequestMonitoringDetail" scope="request">
<aop:scoped-proxy />
</bean>
The scope of the RequestMonitoringDetail bean is request, so a new instance will be created for each request and used by the proxy injected in the filter. RequestMonitoringDetail bean 的 scope 是请求,因此将为每个请求创建一个新实例,并由注入过滤器的代理使用。 I thought it was thread safe.
我认为这是线程安全的。
The problem is a get a NullPointerException from time to time when calling a method on the proxy:问题是在代理上调用方法时不时出现 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$$6c595aed.getShortLogDetailSuffixesForMonitoring(<generated>)
at ...
How could this happen?这怎么可能发生?
Finally I have found the cause: tomcat was shutting down.最后我找到了原因:tomcat 正在关闭。 Just before the error, there was a signal to shut down:
就在错误之前,有一个关闭信号:
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"]
Don't know yet from where it comes, but it seems to be the cause of the errors after.还不知道它来自哪里,但它似乎是之后错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.