The spring, whose spring? My spring!

Source: Internet
Author: User

Spring is familiar in the Java world. Not long ago, as a side dish, I still dislike his cumbersome, even think he is superfluous.

"I don't know what to do with him, except to bring us a bunch of configuration files. "

But it seems to have been said that existence is reasonable.

Also, he can be so popular, there must be a reason for him, not to mention he seems to be more and more fire. (Spring family barrels have come to us)

Spring framework, in fact, he is a tool, tools are serving the service, then he provides what services?

Spring Core Services:

1, control inversion (invesion of controls)

2, face tangent (Aspect oriented programming)

What is control reversal? In fact, it's called Dependency Injection (Dependency injection). To put it simply, the dependency is given to spring management.

Aspect-oriented programming, in fact, is a powerful object-oriented supplement, you can be seen as a black magic, can be no corner, all-in-one embedded program, and no side effects.

Let me tell you a story, once there was a warrior named Spring, who was determined to bring spring to the village's handlers.

He was smart about what the programmers hated and I helped them do it.

Finally, he found that there was a villain called Reliance, programmers hate to deal with him, because he seriously affected the programmer's extension and maintenance work.

So spring invented a beanfactory to use this special factory to manage dependencies for the program.

The way of management is, you want what object to my factory to take, I help you assemble well, handle good dependence.

Gradually, more and more managed programmers, Spring invented the beanfactory seems too primitive.

So spring also invented a applicationcontext, which is a fully functional modern factory with professional people.

From then on, the village of the program members of the shameless have been a happy life.

We will mainly introduce the main characters in the story, first of all, we will look at the protagonist's family spectrum.

Suddenly think of before and product chat, demand how to present things. As a developer I would like to be able to illustrate the requirements and show them all.

Now write a technical blog I can not be good to express themselves, whether it is the use of graphic text or code.

Yes, naïve me, always fantasy a perfect world waiting for me, but always ignore their imperfections.

In reality there will always be some people, may not be so eye-catching, and will not be so distinguished, but they have been silently pay, bear the majority of the Community's responsibility, in them, called Heroes also not too.

Our main analysis is also this kind of hero:

The abstractapplicationcontext of the ApplicationContext family;

Defaultlistablebeanfactory in the Beanfactory family;

Abstractapplicationcontext as the first doer of the family. (The first class, although abstract).

Most of the expectations that the family has bestowed on him have come true. such as beanfactory all implementations of the inheritance system.

in this is the delegate getbeanfactory () implementation. This should have the meaning of bridging inside. Beanfactory Inheritance System full-time implementation of the Bean factory, ApplicationContext system is also a kind of horizontal expansion.

First look at the ApplicationContext boot process (referred to here is the Xmlwebapplicationcontext boot process)

Configurablewebapplicationcontext WAC == sc.getinitparameter (config_location_param); if NULL ) {    wac.setconfiglocation (configlocationparam);} Wac.refresh ();

Yes, applicationcontext after being constructed, set some basic information and then refresh ().

Refresh is what the Doer Abstractapplicationcontext realized.

 Public voidRefresh ()throwsbeansexception, illegalstateexception {synchronized( This. Startupshutdownmonitor) {            //Prepare this context for refreshing.Preparerefresh (); //Tell the subclass to refresh the internal bean factory.Configurablelistablebeanfactory beanfactory =obtainfreshbeanfactory (); //Prepare The Bean Factory for use with this context.preparebeanfactory (beanfactory); Try {                //allows post-processing of the Bean factory in context subclasses.postprocessbeanfactory (beanfactory); //Invoke Factory processors registered as beans in the context.invokebeanfactorypostprocessors (beanfactory); //Register Bean processors that intercept bean creation.registerbeanpostprocessors (beanfactory); //Initialize Message source for this context.Initmessagesource (); //Initialize Event Multicaster for this context.Initapplicationeventmulticaster (); //Initialize Other special beans in specific context subclasses.Onrefresh (); //Check for listener beans and register them.registerlisteners (); //Instantiate all remaining (Non-lazy-init) singletons.finishbeanfactoryinitialization (beanfactory); //Last step:publish corresponding event.Finishrefresh (); }            Catch(Beansexception ex) {if(logger.iswarnenabled ()) {Logger.warn ("Exception encountered during context initialization-" + "Cancelling refresh attempt:" +ex); }                //Destroy already created singletons to avoid dangling resources.Destroybeans (); //Reset ' active ' flag.CancelRefresh (ex); //Propagate exception to caller.                Throwex; }            finally {                //Reset Common introspection caches in Spring ' s core, since we//might not ever need metadata for singleton beans anymore ...resetcommoncaches (); }        }    }

here is the main beanfactory, and then the initialization of a number of related tasks. The highlight is obtainfreshbeanfactory.

Obtainfreshbeanfactory through Refreshbeanfactory to get the real beanfactory.

Its main task is to get defaultlistablebeanfactory, initialize him, and load the beandefinition into beanfactory according to the configuration.

We also post the relevant code

protected Final voidRefreshbeanfactory ()throwsbeansexception {if(Hasbeanfactory ()) {Destroybeans ();        Closebeanfactory (); }        Try{defaultlistablebeanfactory beanfactory=createbeanfactory ();            Beanfactory.setserializationid (GetId ());            Customizebeanfactory (beanfactory);            Loadbeandefinitions (beanfactory); synchronized( This. Beanfactorymonitor) {                 This. Beanfactory =beanfactory; }        }        Catch(IOException ex) {Throw NewApplicationcontextexception ("I/O error parsing bean definition source for" +GetDisplayName (), ex); }    }

Beandefinition is also an important role to be said, as the definition of a class, abstract the basic data of the class and some dependencies.
Specific how to load, according to different forms, (such as XML, annotation scanning, etc.), there will be different implementations, but the details are different, not much elaborated.

Not finished ...

The spring, whose spring? My spring!

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.