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.
Here is the filter code:
public class RequestLoggerFilter extends GenericFilterBean {
@Inject
private RequestMonitoringDetail monitoringDetail;
Here is the proxied bean config:
<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.
I thought it was thread safe.
The problem is a get a NullPointerException from time to time when calling a method on the proxy:
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?
question from:
https://stackoverflow.com/questions/65850344/is-thread-safe-to-inject-an-aopscoped-proxy-with-scope-request-bean-in-a-sing 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…