Implementation Principles of bean types in spring2.0

Source: Internet
Author: User
We know that in spring2.0, there are two types of beans except singleton and prototype. By default, three types of beans are added: request, session, and global session. The added beans are mainly used in Web applications. This article does not want to analyze the usage of the three types of beans, but simply analyzes the implementation principle of the framework.
Spring2.0 adds a scope interface to indicate the bean range.
Public interface scope {
Object get (string name, objectfactory); // obtain a bean instance by name and by creating a factory
Object remove (string name); // deletes a bean with the specified name.
}

The configurablebeanfactory interface of the container defines the Bean factory-related methods for scope registration, so that new types of beans can be added to the bean factory.
Public interface configurablebeanfactory extends hierarchicalbeanfactory,
Void registerscope (string scopename, scope); // Add a new range to the Bean Factory (default: singleton and prototype)
Void destroyscopedbean (string beanname); // destroy the bean in the EAN factory of B
}

The getbean method of abstractfactorybean supports specific scope beans. The core code digest is as follows:

String scopename = mergedbeandefinition. getscope (); // gets the range of the current bean, that is, scope = "request" in the definition.
Scope scope = (scope) This. scopes. Get (scopename); // obtain the range processor in the Bean factory.
If (scope = NULL ){
Throw new illegalstateexception ("no scope registered for scope '" + scopename + "'");
}
Try {
// Use scope. Get (beanname, objectfactory) to obtain or create a bean instance from a specified range
Object scopedinstance = scope. Get (beanname, new objectfactory (){
Public object GetObject () throws beansexception {
Beforeprototypecreation (beanname); // intercept
Try {
Return createbean (beanname, mergedbeandefinition, argS); // call the createbean of the subclass to create a real bean.
}
Finally {
Afterprototypecreation (beanname); // subsequent Interception
}
}
});
Bean = getobjectforbeaninstance (scopedinstance, name, mergedbeandefinition); // return the correct type of bean instance
}
Catch (illegalstateexception ex ){
Throw new beancreationexception (beanname, "Scope '" + scopename + "' is not active", ex );
}

By default, only singleton and prototype types are supported in the lower-layer bean factory. When the scope is set to request and Session, the error that cannot be correctly recognized by the scope will occur. This is because no common Bean factory has registered a new scope. A new type of bean is registered only in webapplicationcontext.
The code for registering a scope is as follows:
Define constants in webapplicationcontext
Public interface webapplicationcontext extends applicationcontext {
String scope_request = "request ";
String scope_session = "session ";
String scope_global_session = "globalsession ";
}

Then, in the implementation of all types of Web application context, the postprocessbeanfactory method is used to register the new type scope during the loading blocking process of the Bean factory, such as genericwebapplicationcontext, staticwebapplicationcontext, and abstractrefreshablewebapplicationcontext.
Protected void postprocessbeanfactory (configurablelistablebeanfactory beanfactory ){
Beanfactory. registerscope (scope_request, new requestscope (); // register a bean of the Request type.
Beanfactory. registerscope (scope_session, new sessionscope (false); // register a Session Bean
Beanfactory. registerscope (scope_global_session, new sessionscope (true); // register the bean of glogalsession
}

Based on the above Code, we should understand why three types of beans can be added only in the Web application context. Of course, with scope, we can easily implement our own scope, add new custom bean types, and then design a bean factory suitable for ourselves.

 

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.