A good memory is better than a bad pen. 83-spring3 Study (4)-spring beanfactory (IOC container)

Source: Internet
Author: User

We generally call beanfactory the IOC container, called ApplicationContext, as the application context (or spring container)
Beanfactory is the core of the spring framework, implementing dependency Injection "making dependencies from a component separate from code, using a configuration file to implement this dependency" and the management of the Bean declaration cycle.

Beanfactory "IOC container" boot process: divided into two stages, one is the container start phase, the other is the bean instantiation phase
Container start-up phase: Load configuration--"Analyze configuration information--" equipment to beandefinition-– "other post-processing (postprocessor)
Bean instantiation Phase: instantiation Phase--"assembly stage-" life cycle Callback-"object other processing-" Register callback interface

"Container start-up phase": At startup, first load by some way, a section relies on a tool class (Bean Definition Reader) to parse and analyze the loaded configuration, and group the analyzed information into corresponding beandefinition, Finally, these save the beandefinition that the bean defines the necessary information. Register to the appropriate beandefinitionregister.
"Bean instantiation Phase": This step is triggered when a requester explicitly requests an object through the getbean aspect of the container. First, the requested object is checked for initialization, and if not, the requested object is instantiated and dependent on the information provided by the registered beandefinition.
"Initialization of the intervening container"
Spring provides a container extension mechanism called beanfactorypostprocessor, which adds an operation to the first stage of container initiation. So that we can do some extra work on beandefinition. For example, modify the attributes defined in the bean, add additional information for the bean definition, and so on.
To customize the beanfactorypostprocessor. You have to implement the Beanfactoryprocessor interface, and if you implement multiple beanfactorypostprocessor in a container, you also need to implement the ordered interface to specify the order.
Spring internal through the JavaBean property editor to help with other types of string conversion work

Specify propertyeditor: Can be inherited by PropertyEditorSupport

"Container bean instantiation Phase"
When the Getbean method is called for the first time, the activity of the bean instantiation phase does not trigger. The second call will directly fenghui the first instance of the container cache, and if the discovery is not instantiated, the object is instantiated by the Createbean () method,

"Instantiation Process"
Instantiate Bean Object-–> Set object properties-–> Check aware related interfaces and set related dependencies-–>beanpostprocess pre-processing, Check whether it is initializingbean to determine whether to call the Afterpropertiesset method-–> check whether a custom init-method-–> beanpostprocessor post processing is configured-–> Register the necessary destruction-related callback interfaces-–> use-–> to implement disposablebean-–> whether a custom Destory method is configured

"Beanfactory Interface": Use Factory mode to instantiate and distribute various components. The dependency between beans is automatically created based on the configuration file Applicationcontext.xml (or other names) when instantiated.
Beanfactory Interface 5 methods, the main implementation of the class: Xmlbeanfactory
Boolean Containsbean (String str)
Object Getbean (String str)
Object Getbean (String str,class)
Boolean Issingleton (String str)
String[] getaliases (String str)//Alias

Simple analog beanfactory (IOC container) initialization
Packagecom. Spring;import org. Springframework. Beans. Factory. Beanfactory;import org. Springframework. Beans. Factory. XML. Xmlbeanfactory;import org. Springframework. Core. IO. Resource;import org. Springframework. Core. IO. Support. Pathmatchingresourcepatternresolver;import org. Springframework. Core. IO. Support. Resourcepatternresolver;/** * Simply say analog beanfactory initialization (IOC container) * * @author Fan Fangming */public class Easybeanfactory {public static void main (string[] args) {resourcepatternresolver resolver = new Pa Thmatchingresourcepatternresolver ();Resource res = resolver. GetResource("Classpath:bean.xml");beanfactory bf = new Xmlbeanfactory (res);System. out. println("Initialize Beanfactory");Role role = BF. Getbean("Role1", Role. Class);System. out. println("role is ready for use." "+ Role. toString());}}

Configuration of the Bean.xml
———-

 Packagecom.spring;/** * A generic class with multiple constructors * @author Fan Fangming * * */ Public  class Role {    PrivateString name;PrivateString type; Public Role() {System.out.println ("Role: default constructor"); }//Private constructor    Private Role(String name) { This. name = name; System.out.println ("Role: constructor with argument string, I am private."); } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGetType() {returnType } Public void SetType(String type) { This. type = type; }@Override     PublicStringtoString(){return "Role class is called:"+ This. Name; }}

A good memory is better than a bad pen. 83-spring3 Study (4)-spring beanfactory (IOC container)

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.