{"id":2016,"date":"2015-11-19T08:02:52","date_gmt":"2015-11-19T13:02:52","guid":{"rendered":"http:\/\/springframework.guru\/?p=2016"},"modified":"2019-06-16T07:38:28","modified_gmt":"2019-06-16T11:38:28","slug":"spring-beannameaware-interface","status":"publish","type":"post","link":"https:\/\/springframework.guru\/spring-beannameaware-interface\/","title":{"rendered":"Spring BeanNameAware Interface"},"content":{"rendered":"<p>During the creation of Spring Beans, you can obtain the name of the bean by implementing the Spring interface &#8211; <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">BeanNameAware<\/code>. Then during the creation of Spring beans, the Spring Context will inject the name it has set for the bean. This is the name the Spring Bean factory will use as a reference to the bean object.<\/p>\n<p>While you this Spring interface is very easy to implement and use, I&#8217;m not sure you really wish to use it. From the <a href=\"http:\/\/docs.spring.io\/spring\/docs\/current\/javadoc-api\/org\/springframework\/beans\/factory\/BeanNameAware.html\" target=\"_blank\" rel=\"noopener noreferrer\">javadoc<\/a> for the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">BeanNameAware<\/code> interface:<\/p>\n<blockquote><p>Interface to be implemented by beans that want to be aware of their bean name in a bean factory. Note that it is not usually recommended that an object depend on its bean name, as this represents a potentially brittle dependence on external configuration, as well as a possibly unnecessary dependence on a Spring API.<\/p><\/blockquote>\n<p>Generally, I prefer my Spring Beans to be unaware of the Spring environment. If your bean actually needs to know the name set for it within the Spring Context, you&#8217;re likely violating one more more of the <a href=\"http:\/\/springframework.guru\/solid-principles-object-oriented-programming\/\" target=\"_blank\" rel=\"noopener noreferrer\">SOLID Principles of Object Oriented Programming<\/a>.<\/p>\n<h1>Implementing BeanNameAware<\/h1>\n<p>Implementing the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">BeanNameAware<\/code> Interface is very simple, as you can see in the example below.<\/p>\n<h4>NameBean.java<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.lifecyclebeans;\r\n\r\nimport org.springframework.beans.factory.BeanNameAware;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class NameBean implements BeanNameAware {\r\n    @Override\r\n    public void setBeanName(String s) {\r\n        System.out.println(\"Hello, my bean name is -\" + s);\r\n    }\r\n}<\/pre>\n<p>By implementing the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">BeanNameAware<\/code> Interface, we provide a method on our class which will accept the string value of the bean name. In the example above, we simply output the bean name to the console.<\/p>\n\r\n\t<div class=\"df_call_to_action df_content_element cta_round\"  data-cta-class=\"normal\" data-cta-bg-color=\"#D3D3D3\" data-cta-border-color=\"#0F0F0F\">\r\n\t    <div class=\"cta_wrapper\">\r\n\t    \t<div class=\"cta_header\"><h3 style=\"color:#6cb44a !important;\">Free Introduction to Spring Tutorial<\/h3><\/div>\r\n\t\t    <div class=\"cta_content\">Are you new to the Spring Framework? Checkout my Free Introduction to Spring Online Tutorial.<\/div>\r\n\t    <\/div>\r\n\t     \r\n\t    \t<div class=\"cta_button\">\r\n\t<div class=\"btn_warpper position_right\">\r\n\t\t<a class=\" btn btn_s btn_rounded\" href=\"http:\/\/springframework.guru\/introduction-spring-framework-tutorial\/\" title=\"http:\/\/springframework.guru\/introduction-spring-framework-tutorial\/\" target=\"_self\"  data-button-class=\"flat\" data-button-color=\"#6cb44a\" data-button-color-hover=\"#C0C0C0\" data-button-border-color=\"#000000\" data-button-border-color-hover=\"#6cb44a\" data-button-bottom-color=\"#FF8080\" data-button-bottom-color-hover=\"#FF8080\" data-font-color=\"#FFFFFF\" data-font-color-hover=\"#000000\">\r\n\t\t\t\t\t\t\r\n\t\t\t\t<span>Learn More<\/span>\r\n\t\t\t\t\t\t\r\n\t\t<\/a>\r\n\t<\/div>\r\n<\/div>\r\n\t     \r\n\t<\/div>\r\n\n<h2>Demonstration of Using the BeanNameAware Interface<\/h2>\n<p>I have a short YouTube video showing the usage of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">BeanNameAware<\/code> Interface.<\/p>\n<div class=\"lyte-wrapper\" style=\"width:640px;max-width:100%;margin:5px auto;\"><div class=\"lyMe\" id=\"WYL_z5SExXYmdIY\"><div id=\"lyte_z5SExXYmdIY\" data-src=\"\/\/i.ytimg.com\/vi\/z5SExXYmdIY\/hqdefault.jpg\" class=\"pL\"><div class=\"tC\"><div class=\"tT\"><\/div><\/div><div class=\"play\"><\/div><div class=\"ctrl\"><div class=\"Lctrl\"><\/div><div class=\"Rctrl\"><\/div><\/div><\/div><noscript><a href=\"https:\/\/youtu.be\/z5SExXYmdIY\" rel=\"nofollow\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i.ytimg.com\/vi\/z5SExXYmdIY\/0.jpg\" alt=\"\" width=\"640\" height=\"340\" \/><br \/>Watch this video on YouTube<\/a><\/noscript><\/div><\/div><div class=\"lL\" style=\"max-width:100%;width:640px;margin:5px auto;\"><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the creation of Spring Beans, you can obtain the name of the bean by implementing the Spring interface &#8211; BeanNameAware. Then during the creation of Spring beans, the Spring Context will inject the name it has set for the bean. This is the name the Spring Bean factory will use as a reference to [&hellip;]<a href=\"https:\/\/springframework.guru\/spring-beannameaware-interface\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Using Spring's BeanNameAware Interface","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[21],"tags":[],"class_list":["post-2016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"Simanta","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/03\/Banner560x292_03web.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-ww","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2016"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=2016"}],"version-history":[{"count":6,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2016\/revisions"}],"predecessor-version":[{"id":5597,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2016\/revisions\/5597"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4586"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=2016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=2016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=2016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}