SubjectCallable的通话方式:
public V call() throws Exception {
try {
threadState.bind();
return doCall(this.callable);
} finally {
threadState.restore();
}
}
1.绑定方法是必要的,但恢复是为什么?
public void bind() {
SecurityManager securityManager = this.securityManager;
if ( securityManager == null ) {
//try just in case the constructor didn't find one at the time:
securityManager = ThreadContext.getSecurityManager();
}
this.originalResources = ThreadContext.getResources();
ThreadContext.remove();
ThreadContext.bind(this.subject);
if (securityManager != null) {
ThreadContext.bind(securityManager);
}
}
public void restore() {
ThreadContext.remove();
if (!CollectionUtils.isEmpty(this.originalResources)) {
ThreadContext.setResources(this.originalResources);
}
}
2.originalResources用来做什么?每次进入AbstractShiroFilter都会创建一个新主题并调用它的执行方法,原始资源似乎没用。
答案 0 :(得分:0)
一般线程健康。如果线程被重用(非常常见),您需要清理资源。它也有助于垃圾收集。
你去过远足吗?不留痕迹;)