Static initialization of Dispatcherservlet
/*** Name of the class path resource (relative to the Dispatcherservlet class) * that defines Dispatcherservlet ' s default strategy names. */ Private Static FinalString Default_strategies_path = "Dispatcherservlet.properties";Private Static FinalProperties defaultstrategies; Static { //Load Default strategy implementations from properties file. //This is currently strictly internal and not meant to be customized//By application developers. Try{Classpathresource Resource=NewClasspathresource (Default_strategies_path, Dispatcherservlet.class); Defaultstrategies= propertiesloaderutils. LoadProperties (Resource); } Catch(IOException ex) {Throw NewIllegalStateException ("Could not load ' dispatcherservlet.properties ':" +ex.getmessage ()); } }
Configuration file Loading
/*** Load properties from the given resource (in Iso-8859-1 encoding). * @paramresource the resource to load from *@returnThe populated Properties instance *@throwsIOException If loading failed *@see#fillProperties (java.util.Properties, Resource)*/ Public StaticProperties loadProperties (Resource Resource)throwsIOException {Properties props=NewProperties (); Fillproperties (props, Resource); returnprops; }
Property padding:
/*** Fill The given properties from the given resource (in Iso-8859-1 encoding). * @paramprops The Properties instance to fill *@paramresource the resource to load from *@throwsIOException If loading failed*/ Public Static voidFillproperties (Properties props, Resource Resource)throwsIOException {InputStream is=Resource.getinputstream (); Try{String filename=Resource.getfilename (); if(FileName! =NULL&&Filename.endswith (xml_file_extension)) {Props.loadfromxml (IS); } Else{props.load (IS); } } finally{is.close (); } }
What can we get from this?
We can take it directly. As a tool class for reading property files.
Dispatcherservlet's policy initialization
Initialize the policy code:
/** * Initialize The strategy objects that this servlet uses. * <p>may is overridden in subclasses in order to initialize further strategy objects. */ protected void initstrategies (ApplicationContext context) { initmultipartresolver (context); Initlocaleresolver (context); Initthemeresolver (context); Inithandlermappings (context); Inithandleradapters (context); Inithandlerexceptionresolvers (context); Initrequesttoviewnametranslator (context); Initviewresolvers (context); Initflashmapmanager (context); }
Let's look at one more of this method structure diagram:
From the diagram above we can see that they all work together in one way:
/*** Create A List of default strategy objects for the given strategy interface. * <p>the default implementation uses the "Dispatcherservlet.properties" file (in the same * package as the DISPA Tcherservlet Class) to determine the class names. It instantiates * The strategy objects through the context ' s beanfactory. * @paramcontext the current Webapplicationcontext *@paramstrategyinterface The Strategy interface *@returnThe List of corresponding strategy objects*/@SuppressWarnings ("Unchecked") protected<T> list<t> getdefaultstrategies (applicationcontext context, class<t>strategyinterface) {String key=Strategyinterface.getname (); String value=Defaultstrategies.getproperty (key); if(Value! =NULL) {string[] classnames= stringutils. Commadelimitedlisttostringarray (value); List<T> strategies =NewArraylist<t>(classnames.length); for(String classname:classnames) {Try{Class<?> Clazz =classutils. forname (ClassName, Dispatcherservlet.class. getClassLoader ()); Object Strategy=createdefaultstrategy (context, clazz); Strategies.add ((T) strategy); } Catch(ClassNotFoundException ex) {Throw NewBeaninitializationexception ("Could not find Dispatcherservlet ' s default Strategy class [" + ClassName + "] for inte Rface ["+ Key +"] ", ex); } Catch(Linkageerror err) {Throw NewBeaninitializationexception ("Error loading Dispatcherservlet ' s default Strategy class [" + ClassName + "] for Inter Face ["+ key +"]: Problem with class file or dependent class ", err); } } returnstrategies; } Else { return NewLinkedlist<t>(); } }
StringUtils and Classutils can be a rich code base OH.
Further, we can find all the *utils.java classes from the Spring repository, discover a huge code base, learn from and re-use this kind of tool library, we can improve our code base and speed up the development efficiency.
Spring MVC dispatcherservlet detailed utils tool class