1 16 17 package org.springframework.web.context.request; 18 19 import org.springframework.beans.factory.ObjectFactory; 20 import org.springframework.beans.factory.config.Scope; 21 22 37 public abstract class AbstractRequestAttributesScope implements Scope { 38 39 public Object get(String name, ObjectFactory objectFactory) { 40 RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); 41 Object scopedObject = attributes.getAttribute(name, getScope()); 42 if (scopedObject == null) { 43 scopedObject = objectFactory.getObject(); 44 attributes.setAttribute(name, scopedObject, getScope()); 45 } 46 return scopedObject; 47 } 48 49 public Object remove(String name) { 50 RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); 51 Object scopedObject = attributes.getAttribute(name, getScope()); 52 if (scopedObject != null) { 53 attributes.removeAttribute(name, getScope()); 54 return scopedObject; 55 } 56 else { 57 return null; 58 } 59 } 60 61 public void registerDestructionCallback(String name, Runnable callback) { 62 RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); 63 attributes.registerDestructionCallback(name, callback, getScope()); 64 } 65 66 67 75 protected abstract int getScope(); 76 77 } 78 | Popular Tags |