Spring Technical Insider Reading notes (i)

Source: Internet
Author: User
Tags assert

1.BeanFactory: Implements the most basic form of the IOC container.
String Factory_bean_prefix = "&";
Object Getbean (String var1) throws beansexception; The bean with the name specified

<T> T Getbean (String var1, class<t> var2) throws beansexception;

<T> T Getbean (class<t> var1) throws Beansexception;bytype

Object Getbean (String var1, Object ... var2) throws beansexception;

<T> T Getbean (class<t> var1, Object ... var2) throws beansexception;

Boolean Containsbean (String var1), whether the bean containing the specified name

Boolean Issingleton (String var1) throws nosuchbeandefinitionexception;
Whether it is a singleton bean
Boolean Isprototype (String var1) throws nosuchbeandefinitionexception;
is a prototype bean
Boolean Istypematch (String var1, Resolvabletype var2) throws nosuchbeandefinitionexception;
Query whether the class type of the bean with the specified name is a specific class type
Boolean Istypematch (String var1, class<?> var2) throws nosuchbeandefinitionexception;
Class<?> GetType (String var1) throws nosuchbeandefinitionexception;
Gets the class type of the specified name bean
String[] Getaliases (String var1); Gets all aliases for the specified name bean
Xmlbeanfactory:
public class Xmlbeanfactory extends Defaultlistablebeanfactory {
Private final Xmlbeandefinitionreader reader;

Public xmlbeanfactory (Resource Resource) throws Beansexception {
This (resource, (beanfactory) null);
}
Beandefination Source in Resource,
Public Xmlbeanfactory (Resource Resource, Beanfactory parentbeanfactory) throws Beansexception {
Super (Parentbeanfactory);
This.reader = new Xmlbeandefinitionreader (this);
This.reader.loadBeanDefinitions (Resource);
}
}
Programmatic use of IOC containers
Classpathresource res = new Classpathresource ("/applicationcontext-core.xml");
Defaultlistablebeanfactory factory = new Defaultlistablebeanfactory ();
Xmlbeandefinitionreader reader = new Xmlbeandefinitionreader (factory);
Reader.loadbeandefinitions (RES);
The initialization of the 1.1IOC container is initiated by calling the Refresh () method,
This boot consists of three processes: 1. Resource positioning of Beandefination 2. Loading 3. Registration
1). Resource positioning: Refers to the beandefination resource location, the process of looking for data. This is done by the Reaourceloader through a unified resource interface.
2). Beandefination loading. The loading process is to represent the user-defined bean as the data structure inside the IOC container, and this data structure is beandefination.
3). The process of registering these beandefination with the IOC container. This process is done by invoking the implementation of the Beandefinatinregistry interface. is to register the beandefination in the onboarding process with the IOC container and inject the beandefination into a hashmap within the IOC container, which is the hashmap of the IOC container that holds the beandefination data.

Loading and Dependency Injection differences:
Dependency injection typically occurs when an application first requests beans from a container through Getbean, but one exception is the ability to set the Lazyinit property on the bean to complete the bean dependency injection so that it is completed during the initialization phase. Instead of having to wait until the entire initialization is complete, the first time you start Getbean.
1.2 Beandefiantion's resource positioning.
In ApplicationContext Spring has provided us with a series of implementations that load different resource readers, such as Filesystemxmlapplicationcontext, Classpathxmlapplicationcontext,xml
Webapplicationcontext can be loaded into resource from the file system, Class Path, or Web container.

public class Filesystemxmlapplicationcontext extends Abstractxmlapplicationcontext {
Public Filesystemxmlapplicationcontext () {
}

Public Filesystemxmlapplicationcontext (applicationcontext parent) {
Super (parent);
} The
//Configlocation of the constructor contains the file path where Beandefination is located
Public Filesystemxmlapplicationcontext (String Configlocation) throws Beansexception {
This (new string[]{configlocation}, True, (applicationcontext) null);
} The
//Configlocation of the constructor contains the file path where multiple beandefination are located
Public filesystemxmlapplicationcontext (String ... Configlocations) throws Beansexception {
This (configlocations, True, (applicationcontext) null);
}
//Specify your own parent IOC container
Public filesystemxmlapplicationcontext (string[] configlocations, ApplicationContext parent) Throws Beansexception {
This (configlocations, true, parent);
}

Public Filesystemxmlapplicationcontext (string[] configlocations, Boolean refresh) throws Beansexception {
This (Configlocations, refresh, (applicationcontext) null);
}
Call the Refresh method to start the beandefination loading process
Public Filesystemxmlapplicationcontext (string[] configlocations, Boolean refresh, ApplicationContext parent) throws beansexception {
Super (parent);
This.setconfiglocations (configlocations);
if (refresh) {
This.refresh ();
}

}

Protected Resource Getresourcebypath (String path) {
if (path! = null && path.startswith ("/")) {
Path = path.substring (1);
}

return new Filesystemresource (path);
}
}
Loading and parsing of 1.3BeanDefination
Loading process: The process of translating a defined beandefination into an IOC container into a data structure represented within the spring, which is maintained and maintained by a hashmap in the IOC container.
Public Filesystemxmlapplicationcontext (string[] configlocations, Boolean refresh, ApplicationContext parent) throws beansexception {
Super (parent);
This.setconfiglocations (configlocations);
This invokes the container's refresh, which is loaded into the beandefination's entrance.
if (refresh) {
This.refresh ();
}
}
A very important way to start the design of the container, refresh, in Abstractapplicationcontext, describes in detail the process of applicationcontext initialization, such as the beanfactory update, Registration of Messagesource and Postprocessor and so on.
public void Refresh () throws Beansexception, IllegalStateException {
Object var1 = This.startupshutdownmonitor;
Synchronized (this.startupshutdownmonitor) {
This.preparerefresh ();
Configurablelistablebeanfactory beanfactory = This.obtainfreshbeanfactory ();
This.preparebeanfactory (beanfactory);
try {
Set up post-processing for beanfactory
This.postprocessbeanfactory (beanfactory);
Call Beanfactory's post processor
This.invokebeanfactorypostprocessors (beanfactory);
Registers the Bean's post processor, which is called during the bean creation process
This.registerbeanpostprocessors (beanfactory);
Initialize the message source for the context
This.initmessagesource ();
Initialize context event mechanism
This.initapplicationeventmulticaster ();
Initializing other special beans
This.onrefresh ();
Check the Listener bean and register the beans with the container
This.registerlisteners ();
Instantiate all non-lazy-init of a single piece
This.finishbeanfactoryinitialization (beanfactory);
Publish container time, end refresh process
This.finishrefresh ();
} catch (Beansexception var9) {
if (this.logger.isWarnEnabled ()) {
This.logger.warn ("Exception encountered during context initialization-cancelling refresh attempt:" + var9);
}
To prevent Baen resource consumption, in exception handling, destroy a single-piece bean that was previously generated
This.destroybeans ();
This.cancelrefresh (VAR9);
Throw VAR9;
} finally {
This.resetcommoncaches ();
}
}
}
In Refreshbeanfactory, if a container already exists, it needs to be destroyed and closed.
Different forms of beandefination use different beandefinationreader to carry out the data loading work.
For parsing the property properties of the XML file bean, the parsing results are put into propertyvalue and then set to Beandefinationholder.
1.4BeanDefination registration in the IOC container
Private final map<string, beandefinition> beandefinitionmap = new Concurrenthashmap (256);
The process of registering the parsed beandefination to the IOC container's beandefinationmap is done after the load beandefination completes.
First Defaultlistablebeanfactory implementation of the Beandefinationregistry interface, the implementation of this interface completes the beandefination to the container registration, the registration process is not complex, is to set the beandefination to HashMap, it should be noted that encountering the same name beandefination need to allow coverage.
Registering the beandefination process
public static void Registerbeandefinition (Beandefinitionholder definitionholder, beandefinitionregistry Registry) Throws Beandefinitionstoreexception {
Get Beanname
String beanname = Definitionholder.getbeanname ();
Registry.registerbeandefinition (Beanname, Definitionholder.getbeandefinition ());
string[] aliases = definitionholder.getaliases ();
if (aliases! = null) {
string[] Var4 = aliases;
int VAR5 = Aliases.length;
//
for (int var6 = 0; var6 < VAR5; ++var6) {
String alias = Var4[var6];
Registry.registeralias (Beanname, alias);
}
}

}

public void Registerbeandefinition (String beanname, Beandefinition beandefinition) throws beandefinitionstoreexception {
Assert.hastext (Beanname, "Bean name must not be empty");
Assert.notnull (beandefinition, "beandefinition must not being null");
if (beandefinition instanceof abstractbeandefinition) {
try {
((abstractbeandefinition) beandefinition). Validate ();
} catch (Beandefinitionvalidationexception var9) {
throw new Beandefinitionstoreexception (Beandefinition.getresourcedescription (), Beanname, "Validation of Beans" Definition failed ", VAR9);
}
}
Beandefinition oldbeandefinition = (beandefinition) this.beanDefinitionMap.get (beanname);
if (oldbeandefinition! = null) {
if (!this.isallowbeandefinitionoverriding ()) {
throw new Beandefinitionstoreexception (Beandefinition.getresourcedescription (), Beanname, "Cannot register Bean" Definition ["+ beandefinition +"] for beans ' "+ beanname +" ': there is already ["+ Oldbeandefinition +"] bound. ");
}
if (Oldbeandefinition.getrole () < Beandefinition.getrole ()) {
if (this.logger.isWarnEnabled ()) {
This.logger.warn ("Overriding user-defined bean definition for beans '" + beanname + "' with a framework-generated bean defi nition:replacing ["+ Oldbeandefinition +"] with ["+ Beandefinition +"] ");
}
} else if (!beandefinition.equals (oldbeandefinition)) {
if (this.logger.isInfoEnabled ()) {
This.logger.info ("Overriding bean Definition for beans '" + beanname + "' with a different definition:replacing [" + oldbe Andefinition + "] with [" + Beandefinition + "]");
}
} else if (this.logger.isDebugEnabled ()) {
This.logger.debug ("Overriding bean Definition for beans '" + beanname + "' with an equivalent definition:replacing [" + OL Dbeandefinition + "] with [" + Beandefinition + "]");
}
This.beanDefinitionMap.put (Beanname, beandefinition);
} else {
if (this.hasbeancreationstarted ()) {
Map VAR4 = This.beandefinitionmap;
Synchronized (THIS.BEANDEFINITIONMAP) {
This.beanDefinitionMap.put (Beanname, beandefinition);
list<string> updateddefinitions = new ArrayList (this.beanDefinitionNames.size () + 1);
Updateddefinitions.addall (This.beandefinitionnames);
Updateddefinitions.add (Beanname);
This.beandefinitionnames = updateddefinitions;
if (This.manualSingletonNames.contains (Beanname)) {
set<string> updatedsingletons = new Linkedhashset (this.manualsingletonnames);
Updatedsingletons.remove (Beanname);
This.manualsingletonnames = updatedsingletons;
}
}
} else {
Register the beandefination process, beanname to Beandefinitionnames, and Beanname as the key to the map, Save Beandefination as value in Beandefinationmap
This.beanDefinitionMap.put (Beanname, beandefinition);
This.beanDefinitionNames.add (Beanname);
This.manualSingletonNames.remove (Beanname);
}
This.frozenbeandefinitionnames = null;
}
if (oldbeandefinition! = NULL | | This.containssingleton (beanname)) {
This.resetbeandefinition (Beanname);
}

Spring Technical Insider Reading notes (i)

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.