<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=youngm" rel="nofollow">Mike Youngstrom</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9388?redirect=false" rel="nofollow">SPR-9388</a></strong> and commented</p>
<p dir="auto">It appears something changed between 3.1.0 and 3.1.1 with regards to Provider resolution.</p>
<ul dir="auto">
<li>I have a request scoped bean injected into a singleton using a Provider.</li>
<li>When multiple instance of my singleton are created I get the exception below.</li>
</ul>
<p dir="auto">This code works fine in 3.1.0.Final.  Fails when I upgrade to 3.1.1.</p>
<p dir="auto">I've included a test case based on the STS MVC project template.</p>
<p dir="auto">To duplicate:</p>
<ol dir="auto">
<li>Import project into STS</li>
<li>Deploy with Tomcat 7.</li>
<li>Notice Error.</li>
<li>Change spring version in pom.xml to 3.1.0</li>
<li>Restart.  Notice No Error.</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someSingleton2': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.inject.Provider com.test.provider.SomeSingleton.someRequestBean; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRequestBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
	at javax.servlet.GenericServlet.init(GenericServlet.java:160)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3908)
	at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
	at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1350)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1537)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1526)
	at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.inject.Provider com.test.provider.SomeSingleton.someRequestBean; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRequestBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	... 31 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRequestBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:342)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:439)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$0(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:472)
	... 33 more
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
	at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328)
	... 40 more"><pre class="notranslate"><code class="notranslate">ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someSingleton2': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.inject.Provider com.test.provider.SomeSingleton.someRequestBean; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRequestBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
	at javax.servlet.GenericServlet.init(GenericServlet.java:160)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3908)
	at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
	at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1350)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1537)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1526)
	at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.inject.Provider com.test.provider.SomeSingleton.someRequestBean; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRequestBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	... 31 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRequestBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:342)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:439)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$0(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:472)
	... 33 more
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
	at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328)
	... 40 more
</code></pre></div>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.1</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/19643/testRequestProvider.zip" rel="nofollow">testRequestProvider.zip</a> (<em>6.92 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398117666" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13819" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13819/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13819">#13819</a> <code class="notranslate">@Inject</code> Provider or <code class="notranslate">@Autowired</code> ObjectFactory issue with session scoped bean (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto">1 votes, 2 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=gthomps95" rel="nofollow">Greg Thompson</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9181?redirect=false" rel="nofollow">SPR-9181</a></strong> and commented</p>
<p dir="auto">I have a session scoped bean.  It's marked with &lt;aop:scoped-proxy /&gt;.  I'm trying to inject a Provider or ObjectFactory into the session bean using <code class="notranslate">@Inject</code> or <code class="notranslate">@Autowired</code>.  It injects fine the first time the session bean is created.  On the second creation of the session bean (for a 2nd session), I get java.lang.IllegalArgumentException when setting the <code class="notranslate">@Inject</code> or <code class="notranslate">@Autowired</code> property.  It's trying to set the actual bean, instead of a provider or an object factory.</p>
<p dir="auto">I stepped through the code.  The code in the getDependencyType() method of DependencyDescriptor doesn't seem to work as expected when it's setting the value on the second session bean.  Something with the nesting level.  So, I don't get a DependencyProvider as expected in the resolveDependency method of DefaultListableBeanFactory class. The getDependencyType returns the class of the bean, instead of a provider or object factory class that is the type of the property.</p>
<p dir="auto">This worked fine with 3.0.</p>
<hr>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/19995/SPR-9181.tgz" rel="nofollow">SPR-9181.tgz</a> (<em>4.89 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398119000" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14024" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14024/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14024">#14024</a> Regression: javax.inject.Provider resolution too agressive (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398152065" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14264" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14264/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14264">#14264</a> Injection of Provider fails for prototype-scoped beans (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398153775" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14478" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14478/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14478">#14478</a> Automatically created java.inject.Provider cannot be injected into session scoped beans (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto"><strong>Referenced from:</strong> commits <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/1bd775f828ddb1f2919c2a57b1151dc3cca9f14c/hovercard" href="https://github.com/spring-projects/spring-framework/commit/1bd775f828ddb1f2919c2a57b1151dc3cca9f14c"><tt>1bd775f</tt></a>, <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/fbe955ab284f5782b80a852b84933af14c32b006/hovercard" href="https://github.com/spring-projects/spring-framework/commit/fbe955ab284f5782b80a852b84933af14c32b006"><tt>fbe955a</tt></a></p>
<p dir="auto">6 votes, 8 watchers</p>