{"id":2515,"date":"2017-11-06T10:39:46","date_gmt":"2017-11-06T10:39:46","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2515"},"modified":"2019-03-02T13:08:59","modified_gmt":"2019-03-02T13:08:59","slug":"what-is-beanfactory","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/what-is-beanfactory\/","title":{"rendered":"What is BeanFactory?"},"content":{"rendered":"<p><strong>What is BeanFactory?<\/strong><br \/>\nThe BeanFactory is the actual container which instantiates, configures, and manages a number of beans.Let have a look at how spring works<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2017\/11\/spring-container.jpg\" alt=\"\" height=\"354\" width=\"473\"\/><\/p>\n<p><strong>How it Works<\/strong><\/p>\n<ol>\n<li>When the application is Deployed the Spring framework reads the xml file and creates the objects.Those are the objects which you see in the Spring Container<\/li>\n<li>Now when you try to refer any of these objects from the outside object using the new method it will throw an exception since or when you try to create a object using new method, the spring container has no idea about the object which you are trying to access<\/li>\n<li>Now to access the object in the container you will use the BeanFactory Objects<\/li>\n<\/ol>\n<p>BeanFactory is represented by org.springframework.beans.factory.BeanFactory interface.It is the main and the basic  way to access the Spring container.Other ways to access the spring container such as ApplicationContext,ListableBeanFactory, ConfigurableBeanFactory etc. are built upon this BeanFactory interface.<\/p>\n<p><strong>BeanFactory interface defines basic functionality for the Spring Container<\/strong> like<\/p>\n<ol>\n<li>It is built upon Factory Design Pattern<\/li>\n<li>provides DI \/ IOC mechanism for the Spring.<\/li>\n<li>It loads the beans definitions and their property descriptions from some configuration source (for example, from XML configuration  file) .<\/li>\n<li>Instantiates the beans when they are requested like beanfactory_obj.getBean(&#8220;beanId&#8221;).\n<\/li>\n<li>Wire dependencies and properties for the beans according to their configuration defined in configuration source while instantiating the beans.<\/li>\n<li>Manage the bean life cycle by bean lifecycle interfaces and calling initialization and destruction methods.\n<\/li>\n<\/ol>\n<p><strong>Note <\/strong><em> that BeanFactory does not create the objects of beans immediately when it loads the configuration for beans from configuration source.Only bean definitions and their property descriptions are loaded. Beans themselves are instantiated  and their properties are set only when they are requested such as by getBean() method.<\/em><\/p>\n<p><strong>Different BeanFactory Implementations:<\/strong><\/p>\n<p><strong>XmlBeanFactory using Constructor:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nResource res = new FileSystemResource(&quot;c:\/beansconfig.xml&quot;);\r\nBeanFactory bfObj = new XmlBeanFactory(res);\r\nMyBean beanObj= (MyBean) bfObj.getBean(&quot;mybean&quot;);\r\n<\/pre>\n<ol>\n<li>The XmlBeanFactory takes the resource object as Parameter<\/li>\n<li>bfObj points to the Spring Container from which you try to fetch the object<\/li>\n<li>mybean is the ID of the Object specified in the XML File<\/li>\n<li>In the above case  BeanFactory loads the beans lazily.BeanFactory will read bean definition of a bean with id &#8220;mybean&#8221; from beansconfig.xml file, instantiates it and return a reference to that.<\/li>\n<li>There are tow implementation of <strong>Resource<\/strong> Intefrace. one is <em>org.springframework.core.io.FileSystemResource<\/em> as seen above and other is <em>org.springframework.core.io.ClassPathResource<\/em> which loads Loads the resource from classpath(shown below). <\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nClassPathResource resorce = new ClassPathResource (&quot;beansconfig.xml&quot;);\r\nBeanFactory factory = new XmlBeanFactory(resource);\r\n<\/pre>\n<p><strong>ClassPathXmlApplicationContext:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(\r\n        new String&#x5B;] {&quot;applicationContext.xml&quot;, &quot;applicationContext-part2.xml&quot;});\r\n\r\n\/\/an ApplicationContext is also a BeanFactory.\r\nBeanFactory factory = (BeanFactory) appContext;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2019\/03\/ACBF.jpg\" alt=\"null\" height=\"478\" width=\"336\" \/><\/p>\n<p><strong>Note <\/strong><em>BeanFactory is not recomended for use in latest Spring versions. It is there only for backward compatability. ApplicationContext is preferred over this because ApplicationContext provides more advance level features which makes an application enterprise level application.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is BeanFactory? The BeanFactory is the actual container which instantiates, configures, and manages a number of beans.Let have a look at how spring works How it Works When the application is Deployed the Spring framework reads the xml file and creates the objects.Those are the objects which you see in the Spring Container Now&hellip; <a href=\"https:\/\/codethataint.com\/blog\/what-is-beanfactory\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[192,211],"tags":[253],"class_list":["post-2515","post","type-post","status-publish","format-standard","hentry","category-interview","category-theory","tag-interview"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2515"}],"version-history":[{"count":8,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":3133,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2515\/revisions\/3133"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}