java - New to Spring - when/where application context is instantiation all the beans -


hi got code mkyong.com

applicationcontext context =        new classpathxmlapplicationcontext(new string[] {"spring-customer.xml"});      customerservice cust = (customerservice)context.getbean("customerservice"); 

in above code, when/where applicationcontext instantiating bean customerservice configured in file spring-customer.xml this.

<bean id="customerservice" class="com.mkyong.customer.services.customerservice">     <property name="customerdao" ref="customerdao" /> </bean> 

simply when/where new customerservice() happen.

the easiest way answer question add break point in constructor of bean , see happens when debugging.

i've added break point in radom class called crawlermanager random java app.

here bean definition:

<bean id="crawlermanager" class="ro.geopatani.crawlers.crawlermanager"/> 

here got:

debug frames

  1. applicationcontext instantiation in main method follows:

    public static void main (string ... args){     applicationcontext applicationcontext = new classpathxmlapplicationcontext("spring.xml");    } 
  2. because default bean scope in spring singleton point instantion of singleton beans start.

the following method invoked name of bean crawlermanager

public object getbean(string name) throws beansexception {             return dogetbean(name, null, null, false); } 

3. class.newinstance() called , constructor of crawlermanager gets triggered.

what important mention method abstractapplicationcontext.refresh() place bean definitions loaded spring.xml file.

here how happens exactly: debug frames-load beans

here exact code refresh method, can see call obtainfreshbeanfactory():

    public void refresh() throws beansexception, illegalstateexception {     synchronized (this.startupshutdownmonitor) {         // prepare context refreshing.         preparerefresh();          // tell subclass refresh internal bean factory.         configurablelistablebeanfactory beanfactory = obtainfreshbeanfactory();          // prepare bean factory use in context.         preparebeanfactory(beanfactory);          try {             // allows post-processing of bean factory in context subclasses.             postprocessbeanfactory(beanfactory);              // invoke factory processors registered beans in context.             invokebeanfactorypostprocessors(beanfactory);              // register bean processors intercept bean creation.             registerbeanpostprocessors(beanfactory);              // initialize message source context.             initmessagesource();              // initialize event multicaster context.             initapplicationeventmulticaster();              // initialize other special beans in specific context subclasses.             onrefresh();              // check listener beans , register them.             registerlisteners();              // instantiate remaining (non-lazy-init) singletons.             finishbeanfactoryinitialization(beanfactory);              // last step: publish corresponding event.             finishrefresh();         }          catch (beansexception ex) {             // destroy created singletons avoid dangling resources.             destroybeans();              // reset 'active' flag.             cancelrefresh(ex);              // propagate exception caller.             throw ex;         }     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -