SpringMVC source code parsing, springmvc source code
During spring MVC development, you can use @ SessionAttributes to annotate the cache information. In this way, during business development, you do not need to manually store sessions and read data again and again.
1 @Controller2 @RequestMapping("telephones")3 @SessionAttributes(value={"name","degree"},types={Double.class})4 public class AttributeController {5 // ...6 }
SpringMVC involves three concepts:
@ SessionAttributes annotation Definition
Annotation information initialization and container SessionAttributesHanlder
Session operation class SessionAttributeStore, although called store is actually called utils more appropriate
SessionAttributesHanlder scans the methods in the class during initialization, finds out @ SessionAttributes annotations, parses them, saves them to attributeNames and attributeTypes, and updates knownAttributeNames.
If it is saved, you can also perform storeAttributes and isHandlerSessionAttribute later.
When reading and clearing data, all data is indexed by knownAttributeNames and then processed by SessionAttributeStore.
SessionAttributeStore authorizes WebRequest processing for specific session operations. It encapsulates an attribute prefix.
Specific analysis directory:
1. Various definitions: @ SessionAttributes, SessionAttributesHandler, SessionAttributeStore
2. Save session attributes
3. Read session attributes
4. Clear session attributes
1. Various definitions of Popular Science
1.1 let's take a look at the definition of @ SessionAttributes annotation. There are two Configuration Methods: value-defined attribute name and types-defined attribute type, such as Date.
1 package org.springframework.web.bind.annotation; 2 3 @Target({ElementType.TYPE}) 4 @Retention(RetentionPolicy.RUNTIME) 5 @Inherited 6 @Documented 7 public @interface SessionAttributes { 8 9 String[] value() default {};10 Class[] types() default {};11 }
1.2 SessionAttributesHandler
It is actually a bit of container flavor, which can be seen from the attributeNames and attributeTypes attributes.
At the same time, the storage, reading and clearing of corresponding attributes are also maintained. Let's take a look at the construction method, retrieveAttributes, and cleanupAttributes APIs.
Of course, the specific session operations are handled through SessionAttributeStore.
KnownAttributeNames uses ConcurrentHashMap, which indicates thread security.
1 package org. springframework. web. method. annotation; 2 3 public class SessionAttributesHandler {4 // attribute name, corresponding to value 5 private final Set of the annotation <String>AttributeNames= New HashSet <String> (); 6 // data type of the attribute, corresponding to the annotation's types 7 private final Set <Class <?>AttributeTypes= New HashSet <Class <?> (); 8 // cache Configuration attribute, including the attributes obtained by Type Scanning. When this is cleared, you can use this as the index to operate 9 // using a ConcurrentHashMap as a Set10 private final Map <String, Boolean>KnownAttributeNames= NewConcurrentHashMap<String, Boolean> (4); 11 // utils of the specific operation session. I personally think the name is a bit odd. 12 private final SessionAttributeStoreSessionAttributeStore; 13 14 15/** 16 * when instantiating, directly parse the annotation 17 */18 public SessionAttributesHandler (Class <?> HandlerType, SessionAttributeStore sessionAttributeStore) {19 Assert. notNull (sessionAttributeStore, "SessionAttributeStore may not be null."); 20 this. sessionAttributeStore = sessionAttributeStore; 2122 SessionAttributes annotation = AnnotationUtils. findAnnotation (handlerType, SessionAttributes. class );23 if (annotation! = Null ){24 this. attributeNames. addAll (Arrays. asList (annotation. value (); 25 this. attributeTypes. addAll (Arrays. <Class <?> AsList (annotation. types ()));26} 27 28 for (String attributeName: this. attributeNames ){29 this. knownAttributeNames. put (attributeName, Boolean. TRUE );30} 31} 32 33 public boolean hasSessionAttributes () {34 return (this. attributeNames. size ()> 0) | (this. attributeTypes. size ()> 0); 35} 36 37/** 38 * determine whether attributeName39 */40 public boolean isHandlerSessionAttribute (String attributeName, Class <?> AttributeType) {41 if (this. attributeNames. contains (attributeName) | this. attributeTypes. contains (attributeType) {42 this. knownAttributeNames. put (attributeName, Boolean. TRUE); 43 return true; 44} 45 else {46 return false; 47} 48} 49 // save 50 public void storeAttributes (WebRequest request, Map <String,?> Attributes) {} 51 // read 52 public Map <String, Object> retrieveAttributes (WebRequest request) {} 53 Object retrieveAttribute (WebRequest request, String attributeName) {} 54 // clear 55 public void cleanupAttributes (WebRequest request) {} 56 57}
1.3 SessionAttributeStore is used for Attribute operations in the session
An interface is defined here and default implementation is provided.
The interface is very simple. It directly defines the interfaces for saving, reading, and clearing.
DefaultSessionAttributeStore is implemented by default. Based on the implementation interface, a prefix is added for distinguishing and WebRequest processing.
SessionAttributeStore Interface Definition
1 package org.springframework.web.bind.support;2 public interface SessionAttributeStore {3 void storeAttribute(WebRequest request, String attributeName, Object attributeValue);4 Object retrieveAttribute(WebRequest request, String attributeName);5 void cleanupAttribute(WebRequest request, String attributeName);6 7 }
DefaultSessionAttributeStore is mainly a attributeNamePrefix definition, and encapsulates the attribute name getAttributeNameInSession. Others are directly delegated.
1 package org. springframework. web. bind. support; 2 public class attributes implements SessionAttributeStore {3 // attribute name prefix 4 private String attributeNamePrefix = ""; 5 // encapsulate attribute name 6 protected String getAttributeNameInSession (WebRequest request, String attributeName) {7 return this. attributeNamePrefix + attributeName; 8} 9 //... 10}
2. Save session attributes
There are two types of save operations: one is the actual save attribute, and the other is to mark whether it has been processed.
Actually saved attributes are: Set <String> attributeNames and Set <Class <?> AttributeTypes. During saving, you can use the constructor and storeAttributes.
Indicates whether the processing is Map <String, Boolean> knownAttributeNames. If you use the constructor or isHandlerSessionAttribute. storeAttributes to save the processing, the isHandlerSessionAttribute is also called.
KnownAttributeNames is used as an index for reading and clearing, especially when types is used for annotation.
1 package org. springframework. web. method. annotation; 2 3 public class SessionAttributesHandler {4/** 5 * when instantiating, directly parse the annotation 6 */7 public SessionAttributesHandler (Class <?> HandlerType, SessionAttributeStore sessionAttributeStore) {8 Assert. notNull (sessionAttributeStore, "SessionAttributeStore may not be null."); 9 this. sessionAttributeStore = sessionAttributeStore; 10 11SessionAttributes annotation = AnnotationUtils. findAnnotation (handlerType, SessionAttributes. class );12 if (annotation! = Null ){13 this. attributeNames. addAll (Arrays. asList (annotation. value (); 14 this. attributeTypes. addAll (Arrays. <Class <?> AsList (annotation. types ()));15} 16 17 for (String attributeName: this. attributeNames) {18This. knownAttributeNames. put (attributeName, Boolean. TRUE );19} 20} 21 22 public boolean hasSessionAttributes () {23 return (this. attributeNames. size ()> 0) | (this. attributeTypes. size ()> 0); 24} 25 26/** 27 * determine whether attributeName28 */29 public boolean isHandlerSessionAttribute (String attributeName, Class <?> AttributeType ){30 if (this. attributeNames. contains (attributeName) | this. attributeTypes. contains (attributeType) {31 this. knownAttributeNames. put (attributeName, Boolean. TRUE );32 return true; 33} 34 else {35 return false; 36} 37} 38/** 39 * Store a subset of the given attributes in the session. attributes not40 * declared as session attributes via {@ code @ SessionAttributes} are optional * @ param request the current request42 * @ param attributes candidate attributes for session storage43 */44 public void storeAttributes (WebRequest request, map <String,?> Attributes) {45 for (String name: attributes. keySet () {46 Object value = attributes. get (name); 47 Class <?> AttrType = (value! = Null )? Value. getClass (): null; 48 49 if (IsHandlerSessionAttribute (name, attrType)) {50 this.SessionAttributeStore. storeAttribute (Request, name, value); 51} 52} 53} 54 55}
1 package org.springframework.web.bind.support;2 public class DefaultSessionAttributeStore implements SessionAttributeStore {3 public void storeAttribute(WebRequest request, String attributeName, Object attributeValue) {4 String storeAttributeName = getAttributeNameInSession(request, attributeName);5 request.setAttribute(storeAttributeName, attributeValue, WebRequest.SCOPE_SESSION);6 }7 // ...8 }
3. Read session attributes
You can read the corresponding attributes based on WebRequest.
Specific processing logic:
Iteration knownAttributeNames
Delegate sessionAttributeStore Processing
SessionAttributeStore uses WebRequest. SCOPE_SESSION to delegate WebRequest processing.
Filter out the null attribute at the same time.
It seems that the logic is always annoying to describe the delegate, but Spring does encapsulate a concept at each layer.
1 // SessionAttributesHandler 2 public Map<String, Object> retrieveAttributes(WebRequest request) { 3 Map<String, Object> attributes = new HashMap<String, Object>(); 4 for (String name : this.knownAttributeNames.keySet()) { 5 Object value = this.sessionAttributeStore.retrieveAttribute(request, name); 6 if (value != null) { 7 attributes.put(name, value); 8 } 9 }10 return attributes;11 }
1 // DefaultSessionAttributeStore2 public Object retrieveAttribute(WebRequest request, String attributeName) {3 String storeAttributeName = getAttributeNameInSession(request, attributeName);4 return request.getAttribute(storeAttributeName, WebRequest.SCOPE_SESSION);5 }
4. Clear session attributes
Properties are cleared only after SessionStatus. setComplete.
The logic of attributes is almost the same as that of attributes, so I won't explain them. Let's look at the Code directly.
1 // SessionAttributesHandler2 public void cleanupAttributes(WebRequest request) {3 for (String attributeName : this.knownAttributeNames.keySet()) {4 this.sessionAttributeStore.cleanupAttribute(request, attributeName);5 }6 }
1 // DefaultSessionAttributeStore2 public void cleanupAttribute(WebRequest request, String attributeName) {3 String storeAttributeName = getAttributeNameInSession(request, attributeName);4 request.removeAttribute(storeAttributeName, WebRequest.SCOPE_SESSION);5 }