STRUTS2 Core Process Source code Analysis

Source: Internet
Author: User

Initialization of Struts2

Strutsprepareandexecutefilter

Attribute Summary

Protected list<pattern>

Excludedpatterns

Protected executeoperations

Execute

Protected prepareoperations

Prepare


1, the initialization of the process

1. Public void init (filterconfig filterconfig) {

2. initoperations init = new initoperations ();

3.//Package Filterconfig. Method Getinitparameternames the name of the parameter to a string in the list

4. filterhostconfig config = new filterhostconfig (filterconfig);

5.//initialization of struts internal log

6. init.initlogging (config);

7.//Create dispatcher. and initialize, this section below we focus on analyzing, initializing when loading those resources

8. Dispatcher Dispatcher = init.initdispatcher (config);

9. init.initstaticcontentloader (config, dispatcher);

10.//Initialize class attributes: Prepare, execute

Prepare = new prepareoperations (Filterconfig.getservletcontext (), dispatcher);

Execute = new executeoperations (Filterconfig.getservletcontext (), dispatcher);

this. excludedpatterns = init.buildexcludedpatternslist (dispatcher);

14.}

1.1, Package Filterconfig

1. Public class Filterhostconfig implements Hostconfig {

2. private filterconfig config;

3. public filterhostconfig (filterconfig config) {

4. this. config = config;

5.}

6.///According to the param-name of Init-param configuration get Param-value value

7. public string Getinitparameter (string key) {

8. return Config.getinitparameter (key);

9.}

10.//Return list of initialization parameter names

Public iterator<string> getinitparameternames () {

return Makeiterator.convert (Config.getinitparameternames ());

13.}

Public ServletContext Getservletcontext () {

return Config.getservletcontext ();

16.}

17.}

There are only a few lines of code that convert the filter initialization parameter name to iterator, which is the main function of the Filterconfig encapsulation.

Create and initialize dispatcher

1. public Dispatcher initdispatcher (Hostconfig filterconfig) {

2. Dispatcher Dispatcher = Createdispatcher (filterconfig);

3. Dispatcher.init ();

4. return dispatcher;

5.}

Creating dispatcher, reading configuration information in Filterconfig, parsing configuration information, encapsulating it as a map, and then eradicating the servlet context and parameter map constructs dispatcher:

1. Private Dispatcher createdispatcher (hostconfig filterconfig) {

2. map<string, string> params = new hashmap<string, string> ();

3. for (iterator E = Filterconfig.getinitparameternames (); E.hasnext ();)

4. String name = (string) e.next ();

5. String value = filterconfig.getinitparameter (name);

6. Params.put (name, value);

7.}

8. Return new Dispatcher (Filterconfig.getservletcontext (), params);

9.}

The main contents of the dispatcher initialization process are as follows


1. Public void init () {

2. if (ConfigurationManager = null) {

3. ConfigurationManager = new ConfigurationManager (beanselectionprovider.default_bean_name);

4.}

5.//Package Org/apache/struts2/default.properties

6. Init_defaultproperties (); [1]

7.//Package Struts-default.xml,struts-plugin.xml,struts.xml

8. Init_traditionalxmlconfigurations (); [2]

9. Init_legacystrutsproperties (); [3]

10.//encapsulates the Configurationproviders class that the user implements

Init_customconfigurationproviders (); [5]

Initialization parameters for//filter

Init_filterinitparameters (); [6]

Init_aliasstandardobjects (); [7]

15.//Here is the key to loading the data in the configuration represented by the [1-7] object.

Container Container = Init_preloadconfiguration ();

Container.inject (this);

Init_checkweblogicworkaround (container);

19.//Add Dispatcher Listener

if (!dispatcherlisteners.isempty ()) {

For ( Dispatcherlistener l:dispatcherlisteners) {

L.dispatcherinitialized (this);

23.}

24.}

25.}

The related configuration files to load struts2 include default.properties,struts-default.xml,struts-plugin.xml,struts.xml ...

The process of [1-7] is to encapsulate all configuration information as a Configurationproviders object and load all the configuration data when the container is created, and the Init_preloadconfiguration function performs the loading process.

encapsulated as Configurationprovier object

Take Default.properties as an example, the specific encapsulation operation is as follows:

1. private void init_defaultproperties () {

2. Configurationmanager.addconfigurationprovider (new Defaultpropertiesprovider ());

3.}

Configuration file loading process


Dispatcher.java file

1.private containerinit_preloadconfiguration () {

2. //Create configuration management and Container objects

3. Configuration config = configurationmanager.getconfiguration ();

4. Container Container =config.getcontainer ();

5.//Set up multi-language variables

6. Boolean reloadi18n = boolean.valueof (Container.getinstance (String. Class, strutsconstants.struts_i18n_reload));

7. Localizedtextutil.setreloadbundles (reloadi18n);

8. Containerholder.store (container);

9. return container;

10.}

Configurationmanager.java

1. publicsynchronizedConfiguration getconfiguration () {

2.//Create Configuration Management Objects

3. Setconfiguration (Createconfiguration (defaultframeworkbeanname));

4.//Add all profile File Manager to Configuration Manager

5. Configuration.reloadcontainer (Getcontainerproviders ());

6. return configuration;

7.}

8. protected Configuration createconfiguration (String beanname) {

9. returnnewdefaultconfiguration (beanname);

10.}

Defaultconfiguration.java

Start loading Data in provider

1. publicsynchronizedlist<packageprovider> reloadcontainer (list<containerprovider>providers )  {

2.

3. Packagecontexts.clear ();

4. Loadedfilenames.clear ();

5. list<packageprovider>packageproviders =new arraylist<packageprovider> ();

6. Containerproperties props =new containerproperties ();

7. Containerbuilder Builder =new containerbuilder ();

8.//containerbuilder uses reflex mechanisms and injections to establish basic container factory

9. Container Bootstrap =createbootstrapcontainer (providers);

For ( final containerprovider containerprovider:p roviders)

11. {

Bootstrap.inject (Containerprovider);

13.///config file configurationproviders read the respective profile and add the data to the Document object container

Containerprovider.init (this);

15.//Call the Register method to begin loading the parts of the document object that are not grouped together, in Strutsxmlconfigurationprovider as an example load beans and constant, the other provider are different

Containerprovider.register (Builder,props);

17.}

Props.setconstants (builder);

Builder.factory (Configuration. Class,newfactory<configuration> () {

Public Configuration Create (context context)throws Exception {

return defaultconfiguration . this;

22.}

23.});

Actioncontext Oldcontext =actioncontext.getcontext ();

//Set The bootstrap container forthe purposes factory creation

SetContext (bootstrap);

container = Builder.create (false);

SetContext (container);

Objectfactory =container.getinstance (objectfactory. Class);

30.///First process the Containerprovider of the configuration file, load the data under the package node, and put the container into packageproviders

For ( final containerprovider containerprovider:p roviders)

32. {

if (containerproviderinstanceof packageprovider) {

Container.inject (Containerprovider);

35.//Here is the key, which loads all the data under the package node

((Packageprovider) containerprovider). Loadpackages ();

Packageproviders.add ((Packageprovider) containerprovider);

38.}

39.}

40.//handle Packageprovider from plugin, ditto processing data

Set<string>packageprovidernames = Container.getinstancenames (packageprovider. Class);

For ( String name:packageprovidernames) {

Packageprovider provider =container.getinstance (Packageprovider. Class, name);

Provider.init (this);

Provider.loadpackages ();

Packageproviders.add (provider);

47.}

Rebuildruntimeconfiguration ();

return packageproviders ;

50.}

Taking Strutsxmlconfigurationprovider as an example

1. publicvoid loadpackages ()throws configurationexception{

2. List<element> reloads =newarraylist<element> ();

3. Verifypackagestructure ();

4.//Document processing objects

5.

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.