I have a session-scoped bean in Spring that is set within the web context. I have a task that runs as a Callable, and I need access to this bean from within that thread. How should I accomplish this? If I simply attempt autowiring the bean I get the error message:
Scope ‘session’ is not active for the current thread
The session-scoped bean I am injecting looks like this:
<bean id="userInfo" class="com.company.web.UserInfoBean" scope="session">
<aop:scoped-proxy />
</bean>
And the class I am trying to inject it into looks like this:
@Component
@Scope( value = "thread", proxyMode = ScopedProxyMode.TARGET_CLASS )
public class GenerateExportThread implements Callable<String> {
...
// this class contains an @Autowired UserInfoBean
@Autowired
private ISubmissionDao submissionDao;
...
}
Lastly, the Callable is being started up like this:
@Autowired
private GenerateExportThread generateExportThread;
@Autowired
private AsyncTaskExecutor taskExecutor;
public void myMethod() {
...
Future<String> future = taskExecutor.submit( new ThreadScopeCallable<String>( generateExportThread ) );
...
}
The ISubmissionDao implementation gets injected correctly, but not its UserInfoBean because that bean is session-scoped. I am okay with doing some manual code work if necessary to copy the object from one session into another at thread startup time (if this makes sense) but I just don’t know how to go about doing this. Any tips are appreciated. Thanks!
Do manual injection:
Your thread-scoped bean:
On your request thread: