{"id":3119,"date":"2019-02-28T16:58:10","date_gmt":"2019-02-28T16:58:10","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3119"},"modified":"2019-02-28T16:59:12","modified_gmt":"2019-02-28T16:59:12","slug":"bean-definition-to-spring-container","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/bean-definition-to-spring-container\/","title":{"rendered":"Bean definition to Spring Container"},"content":{"rendered":"<p>A Bean definition contains the following piece of information called configuration metadata, which helps the container know the following things.<\/p>\n<p>\u2022 The way a bean should be created.<br \/>\n\u2022 Life cycle details of a bean.<br \/>\n\u2022 Associated Bean dependencies.<\/p>\n<p>The above metadata for the bean configuration is provided as a set of properties or attributes in an XML file (configuration file) which together prepare a bean definition. The following are the set of properties.<\/p>\n<table>\n<thead>\n<tr>\n<th>Properties<\/th>\n<th>Usage<\/th>\n<\/tr>\n<thead>\n<tbody>\n<tr>\n<td>class<\/td>\n<td>In a bean definition, it is a mandatory attribute. It is used to specify the bean class which can be used by the container to create the bean.<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>In a bean definition, this attribute is used to specify the bean identifier uniquely. In XML based configuration metadata for a bean, we use the id and\/or name attributes in order to specify the bean identifier(s).<\/td>\n<\/tr>\n<tr>\n<td>scope<\/td>\n<td>This attribute is used to specify the scope of the objects which are created from a particular bean definition.<\/td>\n<\/tr>\n<tr>\n<td>constructor-arg<\/td>\n<td>In a bean definition, this attribute is used to inject the dependencies.<\/td>\n<\/tr>\n<tr>\n<td>properties<\/td>\n<td>In a bean definition, this attribute is used to inject the dependencies.<\/td>\n<\/tr>\n<tr>\n<td>autowiring mode<\/td>\n<td>In a bean definition, this attribute is used to inject the dependencies.<\/td>\n<\/tr>\n<tr>\n<td>lazy-initialization mode<\/td>\n<td>In a bean definition, a lazy-initialized bean informs the IoC container to create a bean instance only when it is first requested, instead of startup.<\/td>\n<\/tr>\n<tr>\n<td>initialization method<\/td>\n<td>In a bean definition, a callback to be called after all required properties on the bean have been set up by the container.<\/td>\n<\/tr>\n<tr>\n<td>destruction method<\/td>\n<td>In a bean definition, a callback to be used when the container that contains the bean is destroyed.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the following example, we are going to look into an XML based configuration file which has different bean definitions. The definitions include lazy initialization (lazy-init), initialization method (init-method), and destruction method (destroy-method) as shown below. This configuration metadata file can be loaded either through BeanFactory or ApplicationContext <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot;?&gt;\r\n\r\n&lt;beans xmlns = &quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n   xmlns:xsi = &quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n   xsi:schemaLocation = &quot;http:\/\/www.springframework.org\/schema\/beans\r\n   http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\r\n   &lt;!-- A simple bean definition --&gt;\r\n   &lt;bean id = &quot;...&quot; class = &quot;...&quot;&gt;\r\n      &lt;!\u2014 Here will be collaborators and configuration for this bean --&gt;\r\n   &lt;\/bean&gt;\r\n\r\n   &lt;!-- A bean definition which has lazy init set on --&gt;\r\n   &lt;bean id = &quot;...&quot; class = &quot;...&quot; lazy-init = &quot;true&quot;&gt;\r\n      &lt;!-- Here will be collaborators and configuration for this bean --&gt;\r\n   &lt;\/bean&gt;\r\n\r\n   &lt;!-- A bean definition which has initialization method --&gt;\r\n   &lt;bean id = &quot;...&quot; class = &quot;...&quot; init-method = &quot;...&quot;&gt;\r\n      &lt;!-- Here will be collaborators and configuration for this bean --&gt;\r\n   &lt;\/bean&gt;\r\n\r\n   &lt;!-- A bean definition which has destruction method --&gt;\r\n   &lt;bean id = &quot;...&quot; class = &quot;...&quot; destroy-method = &quot;...&quot;&gt;\r\n      &lt;!-- collaborators and configuration for this bean go here --&gt;\r\n   &lt;\/bean&gt;\r\n\r\n   &lt;!-- more bean definitions can be written below --&gt;\r\n   \r\n&lt;\/beans&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Bean definition contains the following piece of information called configuration metadata, which helps the container know the following things. \u2022 The way a bean should be created. \u2022 Life cycle details of a bean. \u2022 Associated Bean dependencies. The above metadata for the bean configuration is provided as a set of properties or attributes&hellip; <a href=\"https:\/\/codethataint.com\/blog\/bean-definition-to-spring-container\/\">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":[188],"tags":[],"class_list":["post-3119","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3119","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=3119"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3119\/revisions"}],"predecessor-version":[{"id":3121,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3119\/revisions\/3121"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}