What is the Basic difference between BeanFactory and ApplicationContext ?



Below are the few differences :

1. BeanFactory will create object of test class at the time of calling getBean() method without load on startup.

If you know about servlets then you will get it.

2. ApplicationContext (Eager container) will create objects while loading the xml file, load on startup which is better. 

ApplicationContext because at run time user need not to wait for object creation if there are 1000s of classes references in the xml file.
             <load-on-startup>0</load-on-startup>

That’s why we create Servlet with load on startup enabled otherwise it may take more time.



At the time of loading XML file, IOC will throw exception after parsing file using SAX parser  if spring.xml is not correct.



Application Flow






If bean scope=”singleton” then only it will create objects at the time of loading the  
XMLdocument else it will not and for every request it will create new objects.
So conclusion is that, if bean scope=”prototype” then behavior of both the container will be same.


SAX parser will pass if our XML file is well formed and valid.
IOC uses java reflection classes to create object of the referenced class given in the xml document as shown in the picture below :





ApplicationFlow






The class should need to be public but spring can access private class  and private constructor as well to create objects.

package test;

import java.lang.reflect.Constructor;

public class Client {
public static void main(String[] args) {
try {
Class c = Class.forName("beans.Test");
Constructor[] con = c.getDeclaredConstructors();
con[0].setAccessible(true);
con[0].newInstance(null);
} catch (Exception e) {

}
}
}




As of now all we done with how to create instances...very first feature of container.










No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...