{"id":6558,"date":"2012-08-27T15:39:17","date_gmt":"2012-08-27T10:09:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6558"},"modified":"2012-08-27T15:39:17","modified_gmt":"2012-08-27T10:09:17","slug":"using-postconstruct-annotation-with-grails-services","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-postconstruct-annotation-with-grails-services\/","title":{"rendered":"Using PostConstruct annotation with Grails Services"},"content":{"rendered":"<p>We can use <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/annotation\/PostConstruct.html\">PostConstruct<\/a> with <a href=\"http:\/\/grails.org\/doc\/2.1.0\/guide\/services.html\">Grails Services<\/a> and injected <a href=\"http:\/\/www.grails.org\/doc\/1.3.7\/guide\/14.%20Grails%20and%20Spring.html#14.2%20Configuring%20Additional%20Beans\">Spring Beans<\/a>. This <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/annotation\/PostConstruct.html\">PostConstruct<\/a> annotation can be used to annotate a method which needs to be executed after dependency injection to perform any initialization.<\/p>\n<p>[code]<br \/>\nimport javax.annotation.PostConstruct<\/p>\n<p>class PostConstructDemoService {<\/p>\n<p>    @PostConstruct<br \/>\n    private void init() {<br \/>\n          println &quot;Initializing&quot;<br \/>\n          \/\/your initialization code goes here. e.g connect to some Messaging Service<br \/>\n     }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Check <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/annotation\/PostConstruct.html\">this<\/a> for more details. We also have <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/annotation\/PreDestroy.html\">PreDestroy<\/a>  annotation.<\/p>\n<p style='padding-top:10px'>\nRegards,<br \/>\nAnkur Tripathi<br \/>\nankur@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can use PostConstruct with Grails Services and injected Spring Beans. This PostConstruct annotation can be used to annotate a method which needs to be executed after dependency injection to perform any initialization. [code] import javax.annotation.PostConstruct class PostConstructDemoService { @PostConstruct private void init() { println &quot;Initializing&quot; \/\/your initialization code goes here. e.g connect to some [&hellip;]<\/p>\n","protected":false},"author":24,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":34},"categories":[7],"tags":[892,4840,889,229,4841,890,893,891],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6558"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6558"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6558\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}