{"id":5550,"date":"2012-05-12T14:07:53","date_gmt":"2012-05-12T08:37:53","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5550"},"modified":"2015-07-09T16:01:21","modified_gmt":"2015-07-09T10:31:21","slug":"overriding-properties-in-a-spring-bean-with-beanfactorypostprocessor","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/overriding-properties-in-a-spring-bean-with-beanfactorypostprocessor\/","title":{"rendered":"Overriding properties in a Spring Bean with BeanFactoryPostProcessor"},"content":{"rendered":"<p>I was going through the source code of one of the Grails Plugins where I found the use of Spring&#8217;s <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/javadoc-api\/org\/springframework\/beans\/factory\/support\/BeanDefinitionRegistryPostProcessor.html\" target=\"_blank\">BeanDefinitionRegistryPostProcessor<\/a> to override some configurations that are available in DataSource.groovy. That involved a complete over riding of a <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/api\/org\/springframework\/beans\/PropertyValue.html\" target=\"_blank\">PropertyValue<\/a> definition.<\/p>\n<p>&nbsp;<\/p>\n<p>It set me thinking about whether there is a way to override bean properties like String, Integer etc in a simpler way after the bean has initialized. In comes <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/javadoc-api\/org\/springframework\/beans\/factory\/config\/BeanFactoryPostProcessor.html\" target=\"_blank\">BeanFactoryPostProcessor<\/a>, which provides a wonderful hook to do just that.<\/p>\n<p>&nbsp;<\/p>\n<p>I created a small grails project and added a class CustomBean which looked like this :<\/p>\n<p>[java]<\/p>\n<p>class CustomBean {<br \/>\n       String customVariable<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>I registered a <em>customBean<\/em> on resources.groovy using<\/p>\n<p>[java]<br \/>\nbeans = {<br \/>\n    customBean(CustomBean) {<br \/>\n        String value = &quot;Test Data From resources.groovy&quot;<br \/>\n        println &quot;Setting value ${value}&quot;<br \/>\n        customVariable = value<br \/>\n    }<\/p>\n<p>    customBeanPostProcessor(CustomBeanPostprocessor)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This sets the initial value of <em>customVariable<\/em> as &#8220;Test Data From resources.groovy&#8221;.<\/p>\n<p>If you are wondering what the customBeanPostprocessor is, that is just what I am going to explain next.<br \/>\nThe bean implements the BeanFactoryPostProcessor interface and overrides the methods which provides us with hooks to modify the properties of the bean.<\/p>\n<p>&nbsp;<\/p>\n<p>The class definition is:<\/p>\n<p>[java]<br \/>\nimport groovy.util.logging.Log4j<br \/>\nimport org.springframework.beans.factory.config.BeanPostProcessor<\/p>\n<p>@Log4j<br \/>\nclass CustomBeanPostprocessor implements BeanPostProcessor{<\/p>\n<p>    @Override<br \/>\n    Object postProcessBeforeInitialization(Object bean, String beanName) {<br \/>\n        return bean<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    Object postProcessAfterInitialization(Object bean, String beanName) {<br \/>\n        if(beanName == &#8216;customBean&#8217;) {<br \/>\n            log.debug(&quot;Setting custom value inside post processor&quot;)<br \/>\n            bean.customVariable = &quot;Set from Post Processor&quot;<br \/>\n        }<br \/>\n        return bean<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>What amazed me is the elegance <a title=\"Grails Developers\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">with which the developers of the framework<\/a> has provided entry points into its internals without forcing the users to shave the yak.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was going through the source code of one of the Grails Plugins where I found the use of Spring&#8217;s BeanDefinitionRegistryPostProcessor to override some configurations that are available in DataSource.groovy. That involved a complete over riding of a PropertyValue definition. &nbsp; It set me thinking about whether there is a way to override bean properties [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[819,4840,4841,820],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5550"}],"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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5550"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5550\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}