Spring 3 -- Concept Chapter
I first use SVN to get spring3 source code:
Its code address is: https://src.springframework.org/svn/spring-framework/
Spring3 the source code's organizational structure:
Build-spring-framework is the entire source code build directory, which is the project's build script, to build spring on its own, you can enter this directory to build with Ant.
Other catalogs can see the various components of spring from the name, such as:
Org.springframework.context is the IOC container source code directory,
ORG.SPRINGFRAMEWORK.AOP is the source code directory for AOP implementations,
The main solution spring offers us is the IOC container and AOP support:
IOC Container series:
General Interface ClassbeanfactoryIs the functional specification of the IOC container, which provides the most basic functional specification of the IOC container.Org.springframework.beans.factory
Interface beanfactory All known subinterfaces:ApplicationContext, Autowirecapablebeanfactory, Configurableapplicationcontext, Configurablebeanfactory, Configurablelistablebeanfactory, Configurableportletapplicationcontext, Configurablewebapplicationcontext, Hierarchicalbeanfactory, Listablebeanfactory, WebapplicationcontextAll known implementing Classes:Abstractapplicationcontext, Abstractautowirecapablebeanfactory, Abstractbeanfactory, Abstractrefreshableapplicationcontext, Abstractrefreshableconfigapplicationcontext, Abstractrefreshableportletapplicationcontext, Abstractrefreshablewebapplicationcontext, Abstractxmlapplicationcontext, Annotationconfigapplicationcontext, Annotationconfigwebapplicationcontext, Classpathxmlapplicationcontext, Defaultlistablebeanfactory, Filesystemxmlapplicationcontext, Genericapplicationcontext, Genericwebapplicationcontext, Genericxmlapplicationcontext, Resourceadapterapplicationcontext, Simplejndibeanfactory, Staticapplicationcontext, StaticListableBeanFactory, Staticportletapplicationcontext, Staticwebapplicationcontext, Xmlbeanfactory, Xmlportletapplicationcontext, Xmlwebapplicationcontext
Based on the interfaces and implementations of these spring-provided basic IOC containers, spring defines beandefinition to manage the various objects in the spring-based application and their dependencies.
beandefinition Abstracts Our definition of beans, the primary data type that makes the container work, and the core data type of the IOC.
(When a user uses a container, you can use the escape character & to get the Factorybean itself, which distinguishes through the container to obtain the object produced by the Factorybean and to obtain the Factorybean itself.) All of the beans in spring are managed by the Beanfactory (IOC container), but for Factorybean, this bean is not a simple bean, his implementation and the Factory mode and adorner pattern in design mode similar. )
Beanfactory Method:
Boolean |
Containsbean (String name) Does this bean factory contain a beans with the given name? More specifically, Isgetbean (java.lang.String) able to obtain a beans instance for the given name? |
String[] |
getaliases (String name) Return the aliases to the given bean name, if any. |
|
Getbean (Class<t> requiredtype) Return the Bean instance this uniquely matches the given object type, if any. |
Object |
Getbean (String name) Return a instance, which may be shared or independent, of the specified bean. |
|
Getbean (String name,class<t> requiredtype) Return a instance, which may be shared or independent, of the specified bean. |
Object |
Getbean (String name,object ... args) Return a instance, which may be shared or independent, of the specified bean. |
Class<?> |
GetType (String name) Determine the type of the bean with the given name. |
Boolean |
Isprototype (String name) Is this bean a prototype? is, would getbean (java.lang.String) always return independent instances? |
Boolean |
Issingleton (String name) Is this bean a shared singleton? Which is, would getbean (java.lang.String) always return the same instance? |
Boolean |
Istypematch (String Name,class TargetType) Check whether the bean with the given name matches the specified type. |
the implementation principle of IOC container Xmlbeanfactroy: Org.springframework.beans.factory.xml
Class xmlbeanfactory
Java.lang.Object
org.springframework.core.SimpleAliasRegistry
Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
Org.springframework.beans.factory.support.FactoryBeanRegistrySupport
Org.springframework.beans.factory.support.AbstractBeanFactory
Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
Org.springframework.beans.factory.support.DefaultListableBeanFactory
Org.springframework.beans.factory.xml.XmlBeanFactory
This IOC container can read beandefinition that is defined in XML form. In Xmlbeanfactroy , a xmlbeandefinitionreader object in the initialization, with this reader object, Beandefinition that are defined in XML have a place to deal with, and the processing of these XML forms of information is done with this xmlbeandefinitionreader .
The function of Xmlbeanfactory is based on the basic container of dfaultlistablebeanfactory, which implements additional functions such as XML reading on the base implementation of this container.
xmlbeanfactory Implementation of the source code:
Private final Xmlbeandefinitionreader reader = new Xmlbeandefinitionreader (this);
/**
* Create a new xmlbeanfactory with the given resource,
* Which must be parsable using DOM.
* @param resource XML resource to load beans definitions from
* @throws Beansexception in case of loading or parsing errors
*/
Public xmlbeanfactory (Resource Resource) throws Beansexception {
This (resource, null);
}
/**
* Create a new xmlbeanfactory with the given input stream,
* Which must be parsable using DOM.
* @param resource XML resource to load beans definitions from
* @param parentbeanfactory Parent Bean Factory
* @throws Beansexception in case of loading or parsing errors
*/
Public Xmlbeanfactory (Resource Resource, Beanfactory parentbeanfactory) throws Beansexception {
Super (Parentbeanfactory);
This.reader.loadBeanDefinitions (Resource);
}
examples of programming using IOC containers:
Classpathresource res = new Classpathresource ("Beans.xml");
Defaultlistablebeanfactory factory = new Defaultlistablebeanfactory ();
Xmlbeanfactroyreader reader = new Xmlbeanfactroyreader (factory);
Reader.loadbeanfinitions (res);
In this way, we can use the factory object to defaultlistablebeanfactory this IOC container. The following steps are required to use the IOC container:
1 Create an abstract resource for the IOC configuration file, which contains the beandefinition definition information;
2) Create a beanfactory;
3 Create a reader to load the beandefinition;
4 The resource location of the definition number reads the configuration information
Applicaitoncontext
Advanced container for Extended beanfactoryOrg.springframework.context
Interface ApplicationContext All superinterfaces:Applicationeventpublisher, Beanfactory, Hierarchicalbeanfactory, Listablebeanfactory, MessageSource, ResourceLoader , ResourcepatternresolverAll known subinterfaces:Configurableapplicationcontext, Configurableportletapplicationcontext, Configurablewebapplicationcontext, WebapplicationcontextAll known implementing Classes:Abstractapplicationcontext, Abstractrefreshableapplicationcontext, Abstractrefreshableconfigapplicationcontext, Abstractrefreshableportletapplicationcontext, Abstractrefreshablewebapplicationcontext, Abstractxmlapplicationcontext, Annotationconfigapplicationcontext, Annotationconfigwebapplicationcontext, Classpathxmlapplicationcontext, Filesystemxmlapplicationcontext, Genericapplicationcontext, Genericwebapplicationcontext, Genericxmlapplicationcontext, Resourceadapterapplicationcontext, Staticapplicationcontext, Staticportletapplicationcontext, Staticwebapplicationcontext, Xmlportletapplicationcontext, Xmlwebapplicationcontext
Applicaitoncontext provides beanfactory features that are not available
initiation of the IOC container:
The initialization of the IOC container includes the Beandefinition resource positioning , loading , and registration of three basic processes.
Spring separates the three processes and uses different modules in the implementation, which gives users more flexibility to reduce or extend these three processes, defining the IOC container that works for you.
The Beandefinition data location is completed by the Resourceloader through a unified resource interface, which provides a unified interface for the use of various forms of beandefinition. For the existence of these beandefinition, for example, the bean definition information in the file system can be abstracted using Filesystemresource, Classpathresource can be used for abstraction in the classpath, and so on. This process is similar to the process of looking for data in a container, just like water in a bucket to find it first.
The second critical part is beandefinition loading, which represents the user-defined bean as the data structure within the IOC container, and the internal structure of the container is beandefinition. In general, this beandefinition is actually an abstraction of the Pojo object in the IOC container, a beandefinition that defines a series of data that can be easily managed by the IOC container for the Pojo object, the Spring bean. That is, Beandefinition is the domain object of spring.
The third process is the process of registering these beandefinition with the IOC container. This process is accomplished by invoking the implementation of the Beandefinition interface, which registers the beandefinition that are parsed during the loading process to the IOC container. It can be seen inside the IOC container to hold these beandefinition data by using a hashmap.
The initialization of the context of the IOC container generally does not contain the implementation of the bean dependency injection. In general, dependency injection occurs when the application first requests a bean from the container through Getbean. But with one exception, there is a pre-configured configuration when using the IOC container, which can be configured, specifically by the Lazyinit attribute in the bean definition information, and with this prebuilt feature, the user can control the initialization process of the container To change the dependency injection of this bean with the Lazyinit attribute set, making the bean Dependency injection complete in advance of the IOC container initialization.