Spring Source: Core Components (LI)

Source: Internet
Author: User

First, the implementation of AOP

Spring proxy object Generation: The purpose of the proxy is to invoke the target method when we can instead execute the Invocationhandler class invoke method, so how to make a fuss on Invocationhandler is the key to spring's implementation of AOP. Spring's proxy class is precisely the Proxyfactorybean,proxyfactorybean that inherits the factory bean, especially when it allows you to customize how objects are created. Of course, the proxy object to be generated dynamically through the proxy class. The following is a timing diagram of the proxy objects that spring creates: After spring creates a proxy object, when you invoke a method on the target object, it will be executed by the proxy to the Invocationhandler class's Invoke method. Here the Jdkdynamicaopproxy class implements the Invocationhandler interface.

Spring is the call interceptor, and spring invokes the sequence diagram of the interceptor as follows:

Ii. the process of initialization of the IOC container

Spring provides a basic implementation of the Beanfactory, xmlbeanfactory the same by using the template schema to get an abstract-abstractbeanfactory to the IOC container, Defaultlistablebeanfactory these abstract classes to provide template services for them. Through resource interface to abstract bean definition data, the parsing of XML definition file is done by delegating to Xmlbeandefinitionreader. A simple demonstration of the IOC container creation process:

New Classpathresource ("Beans.xml"newnew  Xmlbeandefinitionreader (Factory); Reader.loadbeandefinitions (res);  

The code demonstrates the following steps:

    1. Creating an abstract resource for an IOC configuration file
    2. Create a Beanfactory
    3. Put the read configuration information Beandefinitionreader, here is the Xmlbeandefinitionreader configuration to Beanfactory
    4. Reading the configuration information from a defined resource location, the specific parsing process is done by Xmlbeandefinitionreader, which completes the process of loading the bean definition. Our IOC container is set up.

In fact, Spring's IOC container initialization includes the following 3 basic procedures for locating, loading, and registering a Bean definition resource file.

  1. Bean definition resource file positioning: Bean definition Resource file positioning is done by resourceloader through a unified resource interface, the resource interface encapsulates various forms of bean definition resource files into uniform, IOC containers that can be loaded into an object. Through the Resourceloader to complete the location of the resource file position, Defaultresourceloader is the default implementation, while the context itself gives the implementation of Resourceloader, can be from the classpath, file system, URL, and so on, as a resource location. In the case of Xmlbeanfactory as an IOC container, it is necessary to specify the resource defined by the bean, which means that the bean definition file is processed by the IOC container by abstracting it into resource.
  2. Bean Definition Resource File loading: The process of loading a bean definition resource file is to convert the bean configured in the Bean definition resource file into the data structure of the bean managed in the IOC container. The data structure of the bean managed in the Spring IOC is beandefinition,beandefinition is the abstraction of the Pojo object in the IOC container. The container uses Beandefinitionreader to parse the definition information and register the bean information, often using Xmlbeandefinitionreader to parse the bean's XML definition file. The actual process is delegated to beandefinitionparserdelegate to get the bean's definition information, which is represented in spring using the Beandefinition object. This name can make us think of loadbeandefinition,registerbeandefinition these related methods, they are all for processing beandefinitin service.
  3. Registry of bean Definitions: After the IOC container parsing has been beandefinition, it needs to be registered in the IOC container, which is implemented by the IOC implementation of the Beandefinitionregistry interface. The registration process is a HASHMAP maintained within the IOC container to preserve the beandefinition process. This hashmap is the place where the IOC container holds the bean information, and later the operation of the bean is done around this hashmap.

Iii. design of the Spring IOC container

Two main lines: the first main line: from the interface beanfactory to the Hierarchicalbeanfactory, and then to the Configurablebeanfactory, is a major beanfactory design route.

    1. The Beanfactory interface defines the specification of the basic IOC container. The basic definition is to get the bean and get the judgment Bean global scope type and so on.
    2. Hierarchicalbeanfactory, provides getparentbeanfactory, enables Beanfactory to have a parent iocring container management capability, and Containslocalbean (String name) Two methods.
    3. Configurablebeanfactory, mainly defines some configuration functions for beanfactory.

The second main line: The second interface design main line is, with ApplicationContext application context interface as the core interface design, here involves the main interface design has, from Beanfactory to Listablebeanfactory, To ApplicationContext, then to Configurableapplicationcontext or Webapplicationcontext.

  1. The Listablebeanfactory interface inherits the Beanfactory and provides the ability to enumerate the beans, and he is able to enumerate all the beans currently loaded by the Beanfactory: enumerating the names of all the beans or satisfying the names of some type of bean, based on The type returns all Bean objects, and so on. However, it cannot get the singleton Bean loaded by the upper beanfactory.
  2. Hierarchicalbeanfactory is relatively simple, it mainly provides the function of the parent beanfactory, through which it is able to get the parent factory of the current beanfactory (PS: B factory starts and loads the Bean before it starts and loads it in a factory, b is the parent factory of a), which allows the current beanfactory to load the Bean loaded by the parent factory, making up for the listablebeanfactory lack of functionality.
  3. Configurablebeanfactory is a hierarchicalbeanfactory based on the addition of configurable features, including registration aliases, registration of the singleton, such as setting Classloader, whether to cache the Bean Metadata, set TypeConverter, beanpostprocessor, configuration Bean dependencies, and so on. Ps:configurablebeanfactory also inherits the singletonbeanregistry definition of a container that registers singleton beans, which is used to define the registry for the bean instance used for sharing. This interface can be implemented by the Beanfactory implementation to form a unified management Singleton bean style.
  4. Autowirecapablebeanfactory, is mainly to provide automatic Bean auto-binding (or automatic assembly) function. For example, according to the automatic assembly strategy new one bean, for the existing Bean Assembly property dependency, and after the creation of the bean callback function, for the bean set name, Bean Factory, Bean post processor, etc., the bean PO St processor is applied to the initialization of the bean, and so on.
  5. Messagesource is used to support internationalization.
  6. The Resourceloader interface is used to implement different resource loading strategies, and the creation of different resource instances is given to Resourceloader to calculate. In the Resourceloader interface, a method is defined primarily: GetResource (), which obtains the resource instance by providing the resource location parameter.
  7. The Applicationeventpublisher interface acts as the publisher of the event, and ApplicationContext implements the interface, which acts as the publisher of the event.

Iv. Overall framework of Spring

Spring has a total of more than 10 components, but there are only three components in the real core: core, context, and beans. They build the skeleton structure of the whole spring. Without them, there is no way to have AOP, the web and other features of the upper layer.

    1. Spring is the bean-oriented programming (Bop,bean oriented Programming), and the Bean is the real protagonist in spring. The bean works in spring just as the object does for OOP, and without the concept of an object is like no object-oriented programming, there is no bean in spring that does not have the meaning of spring. Spring solves a very critical issue in which the dependency between objects is managed by the configuration file, which is the dependency injection mechanism. The injection relationship is managed in a container called IOC, where the object that is wrapped by the bean is stored in the IOC container. Spring is the purpose of managing these objects and some additional columns by wrapping them in the bean.
    2. The context is a collection of bean relationships, also called the IOC container. If the bean is likened to an actor in a performance, then the context is the stage of the performance.
    3. The core is a tool for discovering, building, and maintaining some of the columns needed for the relationship between each bean, and from this point of view, the core component is called util more understandable.

Five, core components

The bean component is under Spring's Org.springframework.beans package. All the classes under this package solve three things: The bean definition, the bean creation, and the parsing of the bean.

As shown in: The typical factory pattern when the bean is created, his top-level interface is beanfactory, which is the inheritance hierarchy of this factory.

As shown: The bean definition is mainly described in Beandefinition, as described in the hierarchical relationships of these classes. The bean definition is a complete description of all the information in the node that you define in spring's configuration file, including the various child nodes. When spring successfully resolves a defined node, it is transformed into a Beandefinition object inside the spring.

As shown: The parsing process of the bean is very complex and the function is very thin, because there are many places that need to be expanded, and there must be sufficient flexibility to cope with possible changes. The parsing of the bean is mainly the parsing of the spring configuration file. This parsing process is done primarily through the classes in.

Context Component: Context is under Spring's Org.springframework.context package. ApplicationContext is the top-level parent of the context, and in addition to the basic information that identifies an application environment, he inherits five interfaces, five of which are mainly extended by the context function. The following is the class structure diagram of the context, from which you can see that ApplicationContext inherits Beanfactory, which also shows that the principal object that is running in the spring container is the bean.

The subclass of ApplicationContext consists of two main aspects:

    1. Configurableapplicationcontext said that the context is modifiable, that is, in the context of building the user can dynamically add or modify existing configuration information, it has more than one sub-class, which most often use the updatable context, That is, the Abstractrefreshableapplicationcontext class.
    2. Webapplicationcontext, as the name implies, is the context for the web that he can access directly to ServletContext, which typically uses less of this interface.

The next point is to build the context file type, followed by the way to access the context. Such a level constitutes the complete level of the context hierarchy. Overall ApplicationContext has to do the following:

    1. Identify an application environment
    2. Creating Bean Objects with beanfactory
    3. Save Object Relationship Table
    4. Ability to capture various events

The context, as the spring IOC container, basically integrates most of the functionality of spring, or is the basis of most features.

Core component, which is a central component of spring, contains a number of key classes, one of which is an important part of defining how resources are accessed. is a resource-related class structure diagram.

The resource interface encapsulates a variety of possible resource types, that is, the different types of files that are blocked by the user. For the resource provider, how to wrap the resources to other people to use this is also a problem, Resource interface inherits the Inputstreamsource interface, this interface has a getInputStream method, the return is InputStream class. This allows all resources to be obtained through the InputStream class, so it also blocks the providers of resources. The Resourceloader interface masks all of the resource loader's differences, just to implement this interface can load all the resources, his default implementation is Defaultresourceloader.

Context and Resource relationship: context is the loading of resources, parsing and description of the work entrusted to the Resourcepatternresolver class to complete, he is equivalent to a joint person, he put the load of resources, Parsing and resource definitions are integrated to facilitate the use of other components.

Six

Spring Source: Core Components (LI)

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.