Spring Framework 4 Source reading (2)---starting from classpathxmlapplicationcontext

Source: Internet
Author: User

Application initialization Log
15:23:12.790 [main] DEBUG o.s.core.env.standardenvironment-adding [systemproperties] Propertysource with lowest search precedence15:23:12.797 [main] DEBUG o.s.core.env.standardenvironment-adding [systemenvironment] PropertySource with Lowest search precedence15:23:12.797 [main] DEBUG o.s.core.env.standardenvironment-initialized standardenvironment  With propertysources [systemproperties,systemenvironment]//initialization environment15:23:12.803 [main] INFO o.s.c.s.classpathxmlapplicationcontext-refreshing org[email protected]480a6370:startup Date [Mon 25 15:23:12 CST 2014]; Root of context hierarchy15:23:12.861 [main] DEBUG o.s.core.env.standardenvironment-adding [systemproperties] Propertysource with lowest search precedence15:23:12.862 [main] DEBUG o.s.core.env.standardenvironment-adding [ Systemenvironment] Propertysource with lowest search precedence15:23:12.862 [main] DEBUG o.s.core.env.standardenvironment-initialized standardenvironment with propertysources [SystemProperties,systemenvironment]//read xml15:23:12.880 [main] INFO o.s.b.f.xml.xmlbeandefinitionreader-loading XML bean Definitions from class path resource [simplecontext.xml]15:23:12.885 [main] DEBUG O.s.b.f.xml.defaultdocumentloader- Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.documentbuilderfactoryimpl]15:23:12.928 [main] DEBUG O.s.b.factory.xml.beansdtdresolver-found beans DTD [HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS-2.0.DTD] in classpath:spring-beans-2.0.dtd//read beandefinition15:23:12.953 [main] DEBUG o.s.b.f.x.defaultbeandefinitiondocumentreader-loading Bean definitions//Parsing xml15:23:12.971 [main] DEBUG O.s.b.f.x.beandefinitionparserdelegate-no XML ' id ' specified-using ' simplebean ' as bean name and [] as Aliases15:23:12  .986 [main] DEBUG o.s.b.f.x.beandefinitionparserdelegate-no XML ' id ' specified-using ' anotherbean ' as bean name and [] As aliases15:23:12.986 [main] DEBUG o.s.b.f.xml.xmlbeandefinitionreader-loaded 3 bean definitions from location PATtern [simplecontext.xml]//will get beandefined set to Beanfactory 15:23:12.987 [main] DEBUG O.s.c.s.classpathxmlapplicationcontext-bean Factory for org[email protected]480a6370:org.s[email  protected]74bdaaa:defining beans [Simplebean,property,anotherbean]; Root of factory hierarchy//initialization messagesource,i18n using 15:23:13.025 [main] DEBUG O.s.c.s.classpathxmlapplicationcontext- Unable to locate Messagesource with Name ' Messagesource ': Using default [[Email protected]6c5f]//application event hubs 15 : 23:13.029 [main] DEBUG o.s.c.s.classpathxmlapplicationcontext-unable to locate Applicationeventmulticaster with name ' Applicationeventmulticaster ': Using default [ORG.[EMAIL PROTECTED]5629FBC9]

Some beans initialize a simple bean with a property in it, and the test method and the setproperty that require attribute injection.
/** * Basic Bean Convenience Test * Created by Zhangya on 2014/8/13. */public class Simplebean{private static final Logger Logger = Loggerfactory.getlogger (simplebean.class);p rivate Simplebeanproperty property;/** * Simple test method */public void Test () {Logger.info ("Simplebean is loading."); Property.propertytest ();} /** * Set Attribute Property * <p> log * @param properties */public void SetProperty (Simplebeanproperty) {Logger.info (" property is setting. "); This.property = property;}}

A bean that is assigned as a property that contains the initialization method

/** * As a property initialization bean * Created by Zhangya on 2014/8/13. */public class Simplebeanproperty{private static final Logger Logger = Loggerfactory.getlogger ( Simplebeanproperty.class);p rivate String simplevariable = "test567";p ublic simplebeanproperty () {Logger.info (" Simplebeanproperty is loading. "); /** * Property's test method */public void PropertyTest () {Logger.info ("PropertyTest method is invoking.{}", this);} /** * Set Variable * @param simplevariable */public void setsimplevariable (String simplevariable) {this.simplevariable = SimpleVar iable;} /** * Gets the value of the variable * @return The value of the variable */public String getsimplevariable () {return simplevariable;} @Overridepublic String toString () {return ' simplebeanproperty{' + ' simplevariable= ' "+ simplevariable + ' \ ' + '} ';}}

The other One

/** * Another bean for initialization * @author Zhangya * @category com.letume.spring.study.init * @since 2014/8/24 */public class Simplean Otherbean{private static final Logger Logger = Loggerfactory.getlogger (simplebeanproperty.class);p rivate String simplevariable = "test123";p ublic Simpleanotherbean () {Logger.info ("Simpleanotherbean is loading.");} /** * Property's test method */public void Test () {Logger.info ("test method is invoking.{}", this); /** * Set Variable * @param simplevariable */public void setsimplevariable (String simplevariable) {this.simplevariable = SimpleVar iable;} @Overridepublic String toString () {return ' simpleanotherbean{' + ' simplevariable= ' "+ simplevariable + ' \ ' + '} ';}}

Simplecontext.xml applicationcontext configuration file, where the bean is configured using XML form

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd BEAN 2.0//en"        "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS-2.0.DTD" ><beans>    <bean name= "Simplebean"          class= "Com.letume.spring.study.init.SimpleBean" >        <property name= "Property" ><ref local= "property"/> </property>    </bean>    <bean id= "Property" Name= "property"          class= "Com.letume.spring.study.init.SimpleBeanProperty" scope= "prototype"/>    <bean name= "Anotherbean"          class= "Com.letume.spring.study.init.SimpleAnotherBean" scope= "prototype"/ ></beans>

Here is the main function,

Example 1 normal bean initialization call procedure

/** * Simple Spring class Loading method * <pre> * Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (path+ Resource_context); * Simplebean Simplebean = Context.getbean (simplebean.class) * simpleloadersimplebean.test (); * </pre> * Created by mitchz on 2014/8/13. */public class Simpleinit{private static final String PATH = "";p rivate static final String resource_context = "Simplecont Ext.xml ";p ublic static void Main (string[] args) throws interruptedexception{classpathxmlapplicationcontext context = New Classpathxmlapplicationcontext (PATH + resource_context);//Get Simplebeansimplebean Simplebean = Context.getBean (" Simplebean ", Simplebean.class); Simplebean.test ();//context.registershutdownhook ();}}

Look at the log:
---Perform the initialization of a singleton (why a singleton is used) 14:36:48.766 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-pre-instantiating singletons in org.s[email protected]29d8a2c5:defining beans [Simplebean,property,anotherbean]; Root of Factory hierarchy14:36:48.767 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-creating shared instance of Sing Leton Bean ' simplebean '---create bean instance 14:36:48.767 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-creating instance of Bean ' Simplebean ' 14:36:48.786 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-eagerly caching bean ' Simplebean ' to Al Low for resolving potential circular references---Create an instance of a property 14:36:48.799 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-creating instance of Bean ' property ' 14:36:48.799 [main] INFO   C.l.s.study.init.simplebeanproperty-simplebeanproperty is loading.14:36:48.799 [main] DEBUG O.s.b.f.s.defaultlistablebeanfactory-finished creating instance of Bean ' property '---assignment 14:36:48.838 [main] INFO   C.l.spring.study.init.simplebEan-property is setting.14:36:48.840 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-finished creating instance of B Ean ' Simplebean ' ....---The Getbean method executes, it is taken directly from the cache, the previously initialized instance 14:36:48.847 [main] DEBUG O.s.b.f.s.defaultlistablebeanfactory-returning cached instance of singleton Bean ' Simplebean ' 14:36:48.847 [main] INFO & Nbsp;c.l.spring.study.init.simplebean-simplebean is loading. Simplebean{property=simplebeanproperty{simplevariable= ' test567 '}}14:36:48.849 [main] INFO   C.l.s.study.init.simplebeanproperty-propertytest method is invoking. simplebeanproperty{simplevariable= ' test567 '}

As you can see from the log, when the bean is not set to scope, the default value is SingletoneOf In addition, even if the attribute class is Protetype, it is also populated with the parent bean initialization. won'tThe bean that the property is associated with is reinitialized when the parent bean is called. See details in Example 2
Example 2, in the course of execution, add property modification, let's take a look at

Modifies the variable simplevariablesimplebean.getproperty () in the property Bean instance. setsimplevariable ("AAAAA");// Retrieve Simplebean Instance Simplebean = Context.getbean ("Simplebean", simplebean.class);//Execute test Method Simplebean.test () again;
Look at the new log:
15:14:58.447 [main] INFO  C.l.spring.study.init.simplebean-simplebean is loading. Simplebean{property=simplebeanproperty{simplevariable= ' AAAAA '}}15:14:58.447 [main] INFO  C.l.s.study.init.simplebeanproperty-propertytest method is invoking. simplebeanproperty{simplevariable= ' AAAAA '}
Looks like our previous guess was right.

First, the bean's scope defaults to Singletone.

Second, the Bean's lazyinit is false.

Third, even if the property is prototype and will no longer be singletone when the parent bean is not re-initialized


Example 3, add two more lines
Get Property Instance Simplebeanproperty property = Context.getbean ("property", simplebeanproperty.class);// Test PropertyTest Method Property.propertytest ();

Then look at the new log after execution:
15:19:10.331 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-creating instance of Bean ' property ' 15:19:10.331 [main] INFO  C.l.s.study.init.simplebeanproperty-simplebeanproperty is loading.15:19:10.331 [main] DEBUG O.s.b.f.s.defaultlistablebeanfactory-finished creating instance of Bean ' property ' 15:19:10.331 [main] INFO  C.l.s.study.init.simplebeanproperty-propertytest method is invoking. simplebeanproperty{simplevariable= ' test567 '}
Because the property's bean was prototype, it was reinitialized.


Example 4, add four more lines:
Get Anotherbean instance Simpleanotherbean Anotherbean = Context.getbean ("Anotherbean", Simpleanotherbean.class); Anotherbean.test ();//Set the value of the variable anotherbean.setsimplevariable ("bbbbb");//Retrieve Anotherbean instance Anotherbean = Context.getbean ("Anotherbean", Simpleanotherbean.class); Anotherbean.test ();

Everyone is looking at the execution log:
15:23:13.130 [Main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-creating instance of Bean ' Anotherbean ' 15:23:13.130 [main] INFO C.l.s.study . Init. Simplebeanproperty-simpleanotherbean is loading.15:23:13.130 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory- Finished creating instance of Bean ' Anotherbean ' 15:23:13.130 [main] INFO c.l.s.study.init.simplebeanproperty-test metho D is invoking. Simpleanotherbean{simplevariable= ' test123 '}15:23:13.131 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory- Creating instance of beans ' Anotherbean ' 15:23:13.131 [main] INFO C.l.s.study.init.simplebeanproperty-simpleanotherbean is loading.15:23:13.131 [main] DEBUG o.s.b.f.s.defaultlistablebeanfactory-finished creating instance of beans ' Anotherbe An ' 15:23:13.131 [main] INFO C.l.s.study.init.simplebeanproperty-test method is invoking. simpleanotherbean{simplevariable= ' test123 '} 
When the bean is prototype, it will be initialized each time by the new
Through the contents of the log, comb the approximate initialization of the logic


Can be seen mainly for beans context and core package.
Specifically how to collaborate with each other, the next section will be further described.

Spring Framework 4 Source reading (2)---starting from classpathxmlapplicationcontext

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.