{"id":3104,"date":"2019-02-27T04:52:39","date_gmt":"2019-02-27T04:52:39","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3104"},"modified":"2019-05-18T07:48:55","modified_gmt":"2019-05-18T07:48:55","slug":"spring-bean-life-cycle","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-bean-life-cycle\/","title":{"rendered":"Spring bean life Cycle"},"content":{"rendered":"<p>Spring Bean life Cycle is hooked to the below 4 interfaces Methods<\/p>\n<ol>\n<li>InitializingBean and DisposableBean callback interfaces<\/li>\n<li>*Aware interfaces for specific behavior<\/li>\n<li>Custom init() and destroy() methods in bean configuration file<\/li>\n<li>@PostConstruct and @PreDestroy annotations<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2019\/05\/1.jpeg\" alt=\"null\" \/><\/p>\n<p><strong>InitializingBean and DisposableBean callback interfaces<\/strong><br \/>\nInitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.<\/p>\n<ol>\n<li>For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set.\n<\/li>\n<li>For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.\n<\/li>\n<\/ol>\n<p>Refer <a href=\"https:\/\/www.mkyong.com\/spring\/spring-initializingbean-and-disposablebean-example\/\">Here<\/a> and <a href=\"https:\/\/www.mkyong.com\/spring\/spring-init-method-and-destroy-method-example\/\">here<\/a><\/p>\n<p><strong>Custom init() and destroy() methods in bean configuration file<\/strong><br \/>\nUsing init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction. <\/p>\n<p><strong>@PostConstruct and @PreDestroy annotations<\/strong><br \/>\nWe can manage lifecycle of a bean by using method-level annotations @PostConstruct and @PreDestroy.<\/p>\n<p>The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.<\/p>\n<p>The @PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container.<\/p>\n<p><strong>@PostConstruct vs init-method vs afterPropertiesSet<\/strong><br \/>\nThere is any difference but there are priorities in the way they work. @PostConstruct, init-method.@PostConstruct is a JSR-250 annotation while init-method is Spring&#8217;s way of having an initializing method.If you have a @PostConstruct method, this will be called first before the initializing methods are called. If your bean implements InitializingBean and overrides afterPropertiesSet, first @PostConstruct is called, then the afterPropertiesSet and then init-method.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Component\r\npublic class MyComponent implements InitializingBean {\r\n\t\r\n\t@Value(&quot;${mycomponent.value:Magic}&quot;)\r\n\tpublic String value;\r\n\t\r\n\tpublic MyComponent() {\r\n\t\tlog.info(&quot;MyComponent in constructor: &#x5B;{}]&quot;, value); \/\/ (0) displays: Null &#x5B;properties not set yet]\r\n\t}\r\n\t\r\n\t@PostConstruct\r\n\tpublic void postConstruct() {\r\n\t\tlog.info(&quot;MyComponent in postConstruct: &#x5B;{}]&quot;, value); \/\/ (1) displays: Magic\r\n\t}\r\n\r\n\t@Override \/\/ (equivalent to init-method in XML; overrides InitializingBean.afterPropertiesSet()\r\n\tpublic void afterPropertiesSet() {\r\n\t\tlog.info(&quot;MyComponent in afterPropertiesSet: &#x5B;{}]&quot;, value);  \/\/ (2) displays: Magic\r\n\t}\t\r\n\r\n        public void initIt() throws Exception {\r\n\t  log.info(&quot;MyComponent in init: &quot; + value);\r\n\t}\r\n\r\n\t@PreDestroy\r\n\tpublic void preDestroy() {\r\n\t\tlog.info(&quot;MyComponent in preDestroy: &#x5B;{}], self=&#x5B;{}]&quot;, value); \/\/ (3) displays: \r\n\t}\r\n\r\n        public void cleanUp() throws Exception {\r\n\t  log.info(&quot;Spring Container is destroy! Customer clean up&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd&quot;&gt;\r\n\t&lt;bean id=&quot;customerService&quot; class=&quot;com.mkyong.customer.services.CustomerService&quot; \r\n\t\tinit-method=&quot;initIt&quot; destroy-method=&quot;cleanUp&quot;&gt;   \t\t\r\n\t\t&lt;property name=&quot;message&quot; value=&quot;i'm property message&quot; \/&gt;\r\n\t&lt;\/bean&gt;\t\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nMyComponent in constructor: [null]\r\nMyComponent in postConstruct: [Magic]\r\nMyComponent in init: [Magic from XML]\r\nMyComponent in afterPropertiesSet: [Magic]\r\nMyComponent in preDestroy: [Magic]\r\nSpring Container is destroy! Customer clean up\r\n<\/pre>\n<p><strong>When to use what?<\/strong><br \/>\n<strong><em>init-method and destroy-method<\/em><\/strong> is the recommended approach because of no direct dependency to Spring Framework and we can create our own methods.<br \/>\n<strong><em>InitializingBean and DisposableBean<\/em><\/strong> To interact with the container&#8217;s management of the bean lifecycle, you can implement the Spring InitializingBean and DisposableBean interfaces. The container calls afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction of your beans.<br \/>\n<strong><em>@PostConstruct and @PreDestroy<\/em><\/strong> &#8211; The JSR-250 @PostConstruct and @PreDestroy annotations are generally considered best practice for receiving lifecycle callbacks in a modern Spring application. Using these annotations means that your beans are not coupled to Spring specific interfaces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Bean life Cycle is hooked to the below 4 interfaces Methods InitializingBean and DisposableBean callback interfaces *Aware interfaces for specific behavior Custom init() and destroy() methods in bean configuration file @PostConstruct and @PreDestroy annotations InitializingBean and DisposableBean callback interfaces InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-bean-life-cycle\/\">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],"tags":[],"class_list":["post-3104","post","type-post","status-publish","format-standard","hentry","category-interview"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3104","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=3104"}],"version-history":[{"count":9,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3104\/revisions"}],"predecessor-version":[{"id":3106,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3104\/revisions\/3106"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}