Struts2 source code reading (4) _ dispatcher & configurationprovider resume

Source: Internet
Author: User

Next Step 3: init_legacystrutsproperties ()
The call is to call the legacypropertiesconfigurationprovider.
Compare the previous defaproperpropertiesprovider and call the legacypropertiesconfigurationprovider.
Defaultpropertiesprovider is inherited from the latter, but the Register () method is rewritten to generate propertiessetting. The former is based on ORG/Apache/struts2/Default. properties.
The latter is based on Struts. Properties
Expand settings. getinstance () in register (), and call getdefainstance instance ()

Private Static settings getdefaultinstance () {<br/> If (defaultimpl = NULL) {<br/> // create Bootstrap implementation <br/> // defaultsettings () without parameters. The difference is that defaproperpropertiesprovider directly includes default. properties parameter <br/> // The default value is struts. and load struts. custom. properties file defined by properties <br/> defaultimpl = new defaultsettings (); </P> <p> // create default implementation <br/> try {<br/> // struts_configur Ation: struts. configuration <br/> // In struts. search for struts in proterties. configuration value, which must be Org. apache. struts2.config. configuration Interface implementation class <br/> // so I am confused about how to convert configuration to setting type in the following conversions... <br/> // This point is put down first, and you have time to study it <br/> string classname = get (strutsconstants. struts_configuration); </P> <p> If (! Classname. equals (defaultimpl. getclass (). getname ())) {<br/> try {<br/> // Singleton instances shouldn't be built accessing request or session-specific context data <br/> defaultimpl = (settings) objectfactory. getobjectfactory (). buildbean (thread. currentthread (). getcontextclassloader (). loadclass (classname), null); <br/>} catch (exception e) {<br/> log. error ("Settings: cocould not instantiate the struts. configuration object, substituting the default implementation. ", e); <br/>}< br/>} catch (illegalargumentexception ex) {<br/> // ignore <br/>}

In 2.1.6, step 4 is removed: init_zeroconfiguration ();
Step 5 is the custom configproviders

Private void init_customconfigurationproviders () {<br/> // you can see that the custom provider is defined on the web. in the param of filterdispatcher in XML: configproviders <br/> string configprovs = initparams. get ("configproviders"); <br/> If (configprovs! = NULL) {<br/> string [] classes = configprovs. split ("// s * [,] // s *"); <br/> for (string cname: classes) {<br/> try {<br/> class CLS = classloaderutils. loadclass (cname, this. getclass (); <br/> configurationprovider prov = (configurationprovider) CLS. newinstance (); <br/> configurationmanager. addconfigurationprovider (PROV); <br/>}< br/>... <br/>}< br/>

Step 6: init_filterinitparameters

// Here we can see that struts. properties can be used not only in struts. XML is defined as constant, and can be defined in the param of filterdispatcher <br/> private void init_filterinitparameters () {<br/> configurationmanager. addconfigurationprovider (New configurationprovider () {<br/> Public void destroy () {}< br/> Public void Init (configuration) throws configurationexception {}< br/> Public void loadpackages () throws configurationexception {}< Br/> Public Boolean needsreload () {return false ;}</P> <p> Public void register (containerbuilder builder, locatableproperties props) throws configurationexception {<br/> props. putall (initparams); // click here to implement the drop ~ <Br/>}< br/>}); <br/>}

Step 7: init_aliasstandardobjects, use beanselectionprovider
This is to map the <bean> defined in the configuration file to the actual class, that is, to inject the bean dependency. This part will sometimes study container later.
 
The next step is to call these configurationproviders.
Expand init_preloadconfiguration ()

Private container init_preloadconfiguration () {<br/> Configuration Config = configurationmanager. getconfiguration (); <br/> Container = config. getcontainer (); </P> <p> Boolean reloadi18n = Boolean. valueof (container. getinstance (string. class, strutsconstants. struts_i18n_reload); <br/> localizedtextutil. setreloadbundles (reloadi18n); </P> <p> return container; <br/>}< br/> // check getconfiguration () Again. <br/> Public synchronized configuration getconfiguration () {<br/> If (configuration = NULL) {<br/> setconfiguration (New defaultconfiguration (defaframeframeworkbeanname )); <br/> try {<br/> // This reloadcontainer focuses on <br/> configuration. reloadcontainer (getcontainerproviders (); <br/>} catch (configurationexception e) {<br/> setconfiguration (null ); <br/> throw new configurationexception ("unable to load configuration. ", e); <br/>}< br/>}else {<br/> conditionalreload (); <br/>}</P> <p> return configuration; <br/>}< br/>

Expand reloadcontainer in defaconfiguration Configuration

Public synchronized list <packageprovider> reloadcontainer (list <containerprovider> providers) throws configurationexception {<br/> packagecontexts. clear (); <br/> loadedfilenames. clear (); <br/> List <packageprovider> packageproviders = new arraylist <packageprovider> (); </P> <p> // struts2 (xwork2) use container to complete the dependency injection function <br/> // first initialize a containerbuilder, then, builder stores the correspondence between interfaces and implementation classes or factory classes. <br/> // then, builder is used. create (BOO Lean) method to generate the iner <br/> // by the container. getinstance (class); then you can get the implementation instance of the interface. <br/> // This part is complicated and will be discussed separately after the research is completed, here, we need to figure out the implementation steps of xwork dependency injection. <br/> containerproperties props = new containerproperties (); <br/> containerbuilder builder = new containerbuilder (); <br/> for (final containerprovider: providers) <br/> {<br/> // call the init and register methods of configurationprovider cyclically, unified and cyclic calling <br/> containerp Rovider. init (this); <br/> containerprovider. register (builder, props); <br/>}< br/> props. setconstants (builder); <br/> // inject dependency, no instance is generated here <br/> builder. factory (configuration. class, new factory <configuration> () {<br/> Public configuration create (context) throws exception {<br/> return defaultconfiguration. this; <br/>}< br/>}); </P> <p> actioncontext oldcontext = actioncontext. getcontext (); <Br/> try {<br/> // set the bootstrap container for the purposes of factory creation <br/> container Bootstrap = createbootstrapcontainer (); <br/> setcontext (bootstrap); <br/> // create a iner with dependency injected <br/> container = builder. create (false); <br/> setcontext (container); <br/> objectfactory = container. getinstance (objectfactory. class); </P> <p> // process the configuration providers first <br/> For (final containerprovider: providers) <br/>{< br/> If (containerprovider instanceof packageprovider) {<br/> container. inject (containerprovider); <br/> // call the loadpackages () method of packageprovider. Here, it is mainly for xmlconfigurationprovider and strutsxmlconfigurationprovider <br/> (packageprovider) containerprovider ). loadpackages (); <br/> packageproviders. add (packageprovider) containerprovider); <BR/>}< br/>}</P> <p> // then process any package providers from the Plugins <br/> set <string> packageprovidernames = container. getinstancenames (packageprovider. class); <br/> If (packageprovidernames! = NULL) {<br/> for (string name: packageprovidernames) {<br/> packageprovider = container. getinstance (packageprovider. class, name); <br/> provider. init (this); <br/> provider. loadpackages (); <br/> packageproviders. add (provider); <br/>}</P> <p> rebuildruntimeconfiguration (); <br/>} finally {<br/> If (oldcontext = NULL) {<br/> actioncontext. setcontext (null); <br/>}< br/> return packageproviders; <br/>}< br/>

Come from: http://qidaoxp.javaeye.com/blog/497052

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.