一、模板方法設計模式設計模式
(1)shiro中受權與訪問控制async
父類AuthorizingRealm方法(模板方法)ui
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) { if(principals == null) { return null; } else { AuthorizationInfo info = null; if(log.isTraceEnabled()) { log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]"); } Cache cache = this.getAvailableAuthorizationCache(); Object key; if(cache != null) { if(log.isTraceEnabled()) { log.trace("Attempting to retrieve the AuthorizationInfo from cache."); } key = this.getAuthorizationCacheKey(principals); info = (AuthorizationInfo)cache.get(key); if(log.isTraceEnabled()) { if(info == null) { log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]"); } else { log.trace("AuthorizationInfo found in cache for principals [" + principals + "]"); } } } if(info == null) { //這是一個抽象方法,要求子類重寫 info = this.doGetAuthorizationInfo(principals); if(info != null && cache != null) { if(log.isTraceEnabled()) { log.trace("Caching authorization info for principals: [" + principals + "]."); } key = this.getAuthorizationCacheKey(principals); cache.put(key, info); } } return info; } }
抽象方法以下this
protected abstract AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection var1);
(2)SpringMVC中的DispatcherServlet extends FramworkServletspa
模版方法debug
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { long startTime = System.currentTimeMillis(); Object failureCause = null; LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext(); LocaleContext localeContext = this.buildLocaleContext(request); RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes requestAttributes = this.buildRequestAttributes(request, response, previousAttributes); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new FrameworkServlet.RequestBindingInterceptor(null)); this.initContextHolders(request, localeContext, requestAttributes); try { //抽象方法,在DispatherServlet中被實現 this.doService(request, response); } catch (ServletException var17) { failureCause = var17; throw var17; } catch (IOException var18) { failureCause = var18; throw var18; } catch (Throwable var19) { failureCause = var19; throw new NestedServletException("Request processing failed", var19); } finally { this.resetContextHolders(request, previousLocaleContext, previousAttributes); if(requestAttributes != null) { requestAttributes.requestCompleted(); } if(this.logger.isDebugEnabled()) { if(failureCause != null) { this.logger.debug("Could not complete request", (Throwable)failureCause); } else if(asyncManager.isConcurrentHandlingStarted()) { this.logger.debug("Leaving response open for concurrent processing"); } else { this.logger.debug("Successfully completed request"); } } this.publishRequestHandledEvent(request, startTime, (Throwable)failureCause); } }
//抽象方法 protected abstract void doService(HttpServletRequest var1, HttpServletResponse var2) throws Exception;