简体   繁体   English

在像 Servlet 或过滤器一样的 singleton bean 中注入 aop:scoped-proxy 与 scope=“request” bean 是否是线程安全的?

[英]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.

相关问题 如何使用构造函数注入将scoped-proxy bean注入到单例bean中 - How to inject a scoped-proxy bean into a singleton bean by using constructor injection 万一我们通过aop作用域代理将原型bean注入到singleton bean中,getter的工作原理(Singleton Bean)是什么? - How getter work (Singleton Bean) in case, we inject prototype bean into singleton bean via aop scoped proxy? 使用AOP作用域代理在单例bean中自动装配原型bean - Using AOP scoped proxy to autowire prototype bean in a singleton bean 将请求范围的Bean注入会话范围的Bean - Inject request scoped bean to session scoped bean 将单例bean注入到请求范围内的JAX-RS资源中 - Inject a singleton bean into a request scoped JAX-RS resource 当请求范围不可用时制作请求范围的 bean 单例 - Make a request scoped bean singleton when request-scope is not available 将请求范围的bean注入另一个bean - Inject request scoped bean into another bean AOP会话作用域代理获取单例作用域 - AOP Session scoped proxy gets singleton scope 如果可能的话,是否可以在请求 scope 中注入 bean,如果它对线程无效,那么注入原型? - Is it possible to inject bean in request scope if possible and if it is not active for thread, then inject prototype? 获取<aop:scoped-proxy />,它是jsp内部的会话作用域 - get <aop:scoped-proxy/> that is session scoped inside of a jsp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM