您可以使用(Holder类以线程绑定的RequestAttributes对象的形式公开Web请求),以使会话作用域的代理bean可用于子线程。
定义自定义ExecuteAndWait拦截并在doIntercept方法使用下面的静态方法从RequestContextHolder
公共静态无效setRequestAttributes(RequestAttributes属性,布尔可继承)
绑定给定RequestAttributes到当前线。
参数: 属性 - 在RequestAttributes暴露,或空重置线程绑定的语境 可继承 - 是否暴露RequestAttributes为可继承的子线程(使用的InheritableThreadLocal)
样品代码
public class CustomExecuteAndWaitInterceptor extends ExecuteAndWaitInterceptor {
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
RequestAttributes requestAtteiAttributes = RequestContextHolder.getRequestAttributes(); //Return the RequestAttributes currently bound to the thread.
RequestContextHolder.setRequestAttributes(requestAtteiAttributes, true);
//do something else if you want ..
return super.doIntercept(actionInvocation);
}
}
当然,当您的子线程需要访问该bean时,会话可能已被用户销毁。可能需要重新审视您的范围。 – 2013-10-29 21:24:56