Simple source code analysis of Spring3.0 Core Components

Source: Internet
Author: User

Preface

This article briefly introduces the source code of Spring3.0. The core components here are not the so-called IOC and AOP, but the start point is the Development Kit released by Spring3.0, of course, Spring3.0 and later versions are basically the same, and the idea remains unchanged. It mainly adds enhancements to various features.

1. Component Overview

Spring3.0 and later versions are organized in different ways, and the official website does not include the dependency packages. To use the dependency packages, you must download the corresponding dependency packages. The basic thumbnails of the SDK are as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/112T94J3-0.jpg "title =" springpack .jpg "/>

You also need a commons-logging-1.0.4.jar log package

The core of Spring is a Bean network built with Bean as the center. It parses and creates beans, integrates other frameworks through beans, and then accesses the beans.

As shown in, the red lines draw the Core components of Spring, namely Core, Context, and beans. Other features, such as jms, orm, and aop, are extended based on these three core components.

The great convenience brought by Spring is to turn the dependency between objects into configuration files, that is, the dependency injection method. The IOC container manages these beans.

The collaboration between the three core components is mainly manifested in Bean packaging the custom Object of our application. The Object contains data, and Context provides a living environment for the data storage, save the mappings between beans and maintain these mappings. Context is a collection of Bean relationships, which is our so-called IOC container. Core is a tool required by Context to discover, establish, and maintain the relationship between beans, such as resource loading and resource abstraction.

Two Bean components

All classes about Bean components exist in org. springframework. beans-3.0.5.RELEASE.jar. This development kit defines Bean creation, Bean parsing, and Bean definition. Many design modes are used here, such as the template method design mode, policy mode, and factory design mode.

Bean creation: Bean creation adopts the factory design mode. Of course, there is also the template method mode.

BeanFactory is the top-level interface created by Bean, and ListableBeanFactory indicates that these beans are list-able,

HierarchicalBeanFactory indicates that these beans are inherited AutowireCapableBeanFactory interfaces that define automatic Bean Assembly rules. These four interfaces jointly define the Bean set, the relationship between beans, and Bean behavior. The final default implementation class is DefaultListableBeanFactory.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/112T933O-1.jpg "title =" BeanFactory related class diagram- 文.jpg "/>

Bean definition is mainly defined by BeanDefinition. Because Spring converts objects to beans for processing, it is necessary to define beans that can be processed by IOC containers, in this way, unified operations can be ensured.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/112T91233-2.jpg "title =" Bean definition- 文.jpg "/>

Bean Parsing is mainly used to parse Spring configuration files and parse the corresponding information to generate Bean objects.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/112T92160-3.jpg "title =" Bean parsing related class diagram- 文.jpg "/>

BeanFactory and FactoryBean:

BeanFactory refers to the programming abstraction of IOC containers, such as ApplicationContext and XmlBeanFactory. These are the specific manifestations of IOC containers and their main responsibilities are: instantiate, locate, configure Bean objects in the application, and establish dependencies between these beans.

FactoryBean is just a bean that can be managed in the IOC container. It is a special Bean, that is, a factory Bean, which can be used to generate other beans, we can regard it as an abstract factory. The product Bean produced by the factory is returned for its call. The returned object is not an object instance of the specified class, it is the object returned by the getObject method of the factory Bean.

Three Context components

The class definitions about the Context component are all in org. springframework. context-3.0.5.RELEASE.jar.

The Context component provides a runtime environment for the Bean object, identifies a runtime environment, initializes BeanFactory, uses BeanFactory to create the parsed registered Bean, and saves the relationship between Bean objects. Context combines the Core and Bean components. The root component of the Context component is ApplicationContext, which inherits both BeanFactory and the Resource class in Core.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/112T96144-4.jpg "title =" Context related class diagram- 文.jpg "/>

Context is to build a Bean network in BeanFactory, which is implemented through the refresh method in AbstractApplicationContext.

It can be seen that although ApplicationContext inherits BeanFactory, many new functions are added. BeanFactory does not have the ability to define resources, and ApplicationContext can define resources by itself, from this perspective, context is more useful. The following functions are added to ApplicationContext:

1) Supports different information sources, which extend MessageSource.

2) access resources are mainly inherited by ResourceLoader, so that bean-defined resources can be obtained from different places.

3) supports application events, inherits ApplicationEventPublisher, and introduces the event mechanism in the context.

The configuricationcontext sub-classes. ableableapplicationcontext indicates that the Context is changeable, that is, you can dynamically add or modify the existing configuration information in the build Context. There are multiple sub-classes below it, among them, the updatable Context is most often used, that is, the AbstractRefreshableApplicationContext class.

WebApplicationContext is mainly used for the Context of the web. It can directly access ServletContext.

Four Core Components

All classes about Core components are in org. springframework. core-3.0.5.RELEASE.jar.

The Core Component mainly defines the way to access resources, and abstracts various resources with unified interfaces, blocking the types of specific resources. The top-level interface of a Resource is Resource, which inherits from InputStreamResource and implements its getInstream method. In this way, all resources obtain the input stream through this method. Resource loading is also implemented in a unified manner, and a top-level resource loading interface ResourceLoader is defined. The default load is DefaultResourceLoader.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/112Ta005-5.jpg "title =" resourcerelated image .jpg "/>

Note that ApplicationContext inherits the resource loading interface, and its subclass AbstractApplicationContext implements DefaultResourceLoader, which is the default class for the system to load resources. FileSystemResourceLoader in the ultresourceloader subclass returns a FileSystemResource object, while ServletContextResourceLoader returns a new ServletContextResource, while AbstractApplicationContext returns a new ClassPathResource for resource access.

This article from the "dream in the cloud" blog, please be sure to keep this source http://computerdragon.blog.51cto.com/6235984/1243013

Related Article

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.