Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

java - 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally I have found the cause: tomcat was shutting down. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...