{"id":1524,"date":"2016-09-01T12:54:25","date_gmt":"2016-09-01T12:54:25","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1524"},"modified":"2016-09-01T14:07:12","modified_gmt":"2016-09-01T14:07:12","slug":"accessing-beans-2","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/accessing-beans-2\/","title":{"rendered":"Accessing Beans 2"},"content":{"rendered":"<p><strong>Reading constant from properties file<\/strong><br \/>\n<strong>constant.properties<\/strong><\/p>\n<pre>\r\nPointA.PointX=-20\r\nPointA.PointY=0\r\n<\/pre>\n<p><strong>spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans&gt;\r\n\t&lt;bean id=&quot;pointA&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;${PointA.PointX}&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;${PointA.PointY}&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;\r\n\t\t&lt;property name=&quot;location&quot; value=&quot;constant.properties&quot;&gt;&lt;\/property&gt;\r\n\t&lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Using Interface<\/strong><br \/>\n<strong>spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans&gt;\r\n\t&lt;bean id=&quot;pointA&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;${PointA.PointX}&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;${PointA.PointY}&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;\r\n\t\t&lt;property name=&quot;center&quot; ref=&quot;pointA&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>shape.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Shape { \r\n\tpublic void drawShape();\r\n}\r\n<\/pre>\n<p><strong>triangle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Triangle implements Shape\r\n{\r\n public void drawShape()\r\n {\r\n   System.out.println(&quot;Shape of Triangle&quot;);\r\n }\r\n}\r\n<\/pre>\n<p><strong>circle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Circle implements Shape{\r\n\tprivate Point center;\r\n\t\r\n\t@Override\r\n\tpublic void drawShape() {\r\n\t\tSystem.out.println(&quot;Shape of Circle &quot;);\r\n\t\tSystem.out.println(&quot;Center of Cirlce &quot;+ getCenter().getX() + &quot; - &quot; + getCenter().getY());\r\n\t}\r\n\r\n\tpublic Point getCenter() {\r\n\t\treturn center;\r\n\t}\r\n\r\n\tpublic void setCenter(Point center) {\r\n\t\tthis.center = center;\r\n\t}\t\t\r\n}\r\n\r\n<\/pre>\n<p><strong>The objShape  will call the draw method based on the instance referenced at runtime<\/strong><br \/>\n<strong>shape.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class DrawingApp {\r\n\tpublic static void main(String&#x5B;] args)  {\r\n\t\t ApplicationContext objContext = new ClassPathXmlApplicationContext(&quot;spring1.xml&quot;);\r\n\t\tShape objShape =  (Shape)objContext.getBean(&quot;circleId&quot;);\r\n\t\tobjShape.drawShape();\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reading constant from properties file constant.properties PointA.PointX=-20 PointA.PointY=0 spring.xml &lt;beans&gt; &lt;bean id=&quot;pointA&quot; class=&quot;com.mugil.shapes.Point&quot;&gt; &lt;property name=&quot;x&quot; value=&quot;${PointA.PointX}&quot;\/&gt; &lt;property name=&quot;y&quot; value=&quot;${PointA.PointY}&quot;\/&gt; &lt;\/bean&gt; &lt;bean class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt; &lt;property name=&quot;location&quot; value=&quot;constant.properties&quot;&gt;&lt;\/property&gt; &lt;\/bean&gt; &lt;\/beans&gt; Using Interface spring.xml &lt;beans&gt; &lt;bean id=&quot;pointA&quot; class=&quot;com.mugil.shapes.Point&quot;&gt; &lt;property name=&quot;x&quot; value=&quot;${PointA.PointX}&quot;\/&gt; &lt;property name=&quot;y&quot; value=&quot;${PointA.PointY}&quot;\/&gt; &lt;\/bean&gt; &lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt; &lt;property name=&quot;center&quot; ref=&quot;pointA&quot;\/&gt; &lt;\/bean&gt; &lt;\/beans&gt; shape.java public interface Shape { public&hellip; <a href=\"https:\/\/codethataint.com\/blog\/accessing-beans-2\/\">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":[189],"tags":[],"class_list":["post-1524","post","type-post","status-publish","format-standard","hentry","category-form-elements-spring"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1524","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=1524"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1524\/revisions"}],"predecessor-version":[{"id":1527,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1524\/revisions\/1527"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}