{"id":1506,"date":"2016-08-31T13:04:44","date_gmt":"2016-08-31T13:04:44","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1506"},"modified":"2016-08-31T16:22:32","modified_gmt":"2016-08-31T16:22:32","slug":"1506","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/1506\/","title":{"rendered":"Working with Bean"},"content":{"rendered":"<p><strong>spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;!DOCTYPE beans PUBLIC &quot;-\/\/SPRING\/\/DTD BEAN\/\/EN&quot; &quot;http:\/\/www.springframework.org\/dtd\/spring-beans.dtd&quot;&gt;\r\n&lt;beans&gt;\r\n    &lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Triangle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\npublic class Triangle {\r\n\tprivate String type;\r\n\t\r\n\tpublic String getType() {\r\n\t\treturn type;\r\n\t}\r\n\r\n\tpublic void setType(String type) {\r\n\t\tthis.type = type;\r\n\t}\r\n\r\n\tpublic void drawShape()\r\n\t{\r\n\t\tSystem.out.println(&quot;Shape of Triangle&quot;);\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><strong>Reading value of Bean by Application Context and Bean Factory<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Shape \r\n{\r\n   public static void main(String&#x5B;] args) \r\n   {\r\n\tApplicationContext objContext = new ClassPathXmlApplicationContext(&quot;spring1.xml&quot;);\r\n\tTriangle objTriangle1 =  (Triangle)objContext.getBean(&quot;triangleId&quot;);\r\n\tobjTriangle1.drawShape();\r\n\t\t\r\n\t\t\r\n\tBeanFactory  objBeanFactory = new XmlBeanFactory(new FileSystemResource(&quot;spring.xml&quot;));\r\n\tTriangle objTriangle2 =  (Triangle)objBeanFactory.getBean(&quot;triangleId&quot;);\r\n\tobjTriangle2.drawShape();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Constructor Initialization<\/strong><br \/>\n<strong>Triangle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\npublic class Triangle \r\n{\r\n\tprivate String type;\r\n\r\n        public Triangle(String ptype)\r\n\t{\r\n\t\tthis.type = ptype;\r\n\t}\r\n\t\r\n        public void drawShape()\r\n\t{\r\n\t\tSystem.out.println(&quot;Shape of Triangle&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>The Index Specifies which variable in the Bean is Initialized<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans&gt;\r\n\t&lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot;&gt;\r\n\t\t&lt;constructor-arg index=&quot;0&quot; value=&quot;Isolseles&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Real World Dependency Injection<\/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;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot;&gt;\r\n\t\t&lt;property name=&quot;pointA&quot; ref=&quot;point1&quot;\/&gt;\r\n\t\t&lt;property name=&quot;pointB&quot; ref=&quot;point2&quot;\/&gt;\r\n\t\t&lt;property name=&quot;pointC&quot; ref=&quot;point3&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t\r\n\t&lt;bean id=&quot;point1&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;-20&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;point2&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;0&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;point3&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;20&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>Triangle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\npublic class Triangle {\r\n\tprivate String type;\r\n\tprivate Point pointA;\r\n\tprivate Point pointB;\r\n\tprivate Point pointC;\r\n\t\r\n\tpublic String getType() {\r\n\t\treturn type;\r\n\t}\r\n\r\n\tpublic void setType(String type) {\r\n\t\tthis.type = type;\r\n\t}\r\n\r\n\tpublic void drawShape()\r\n\t{\r\n\t\tSystem.out.println(&quot;Shape of Triangle&quot;);\r\n\t}\r\n\r\n\tpublic Point getPointA() {\r\n\t\treturn pointA;\r\n\t}\r\n\r\n\tpublic void setPointA(Point pointA) {\r\n\t\tthis.pointA = pointA;\r\n\t}\r\n\r\n\tpublic Point getPointB() {\r\n\t\treturn pointB;\r\n\t}\r\n\r\n\tpublic void setPointB(Point pointB) {\r\n\t\tthis.pointB = pointB;\r\n\t}\r\n\r\n\tpublic Point getPointC() {\r\n\t\treturn pointC;\r\n\t}\r\n\r\n\tpublic void setPointC(Point pointC) {\r\n\t\tthis.pointC = pointC;\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><strong>Point.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\npackage com.mugil.shapes;\r\n\r\npublic class Point {\r\n\tprivate int x;\r\n\tprivate int y;\r\n\t\r\n\tpublic int getX() {\r\n\t\treturn x;\r\n\t}\r\n\tpublic void setX(int x) {\r\n\t\tthis.x = x;\r\n\t}\r\n\tpublic int getY() {\r\n\t\treturn y;\r\n\t}\r\n\tpublic void setY(int y) {\r\n\t\tthis.y = y;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Shape.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\nimport org.springframework.beans.factory.BeanFactory;\r\nimport org.springframework.beans.factory.xml.XmlBeanFactory;\r\nimport org.springframework.core.io.FileSystemResource;\r\n\r\npublic class Shape \r\n{\r\n    public static void main(String&#x5B;] args) \r\n    {\t\t\r\n\tBeanFactory  objBeanFactory = new XmlBeanFactory(new FileSystemResource(&quot;spring.xml&quot;));\r\n\tTriangle objTriangle2 =  (Triangle)objBeanFactory.getBean(&quot;triangleId&quot;);\r\n\t\t\r\n\tSystem.out.println(&quot;The Refereeed Points are = &quot;);\r\n\tSystem.out.println(&quot;Point A :&quot; + objTriangle2.getPointA().getX() +&quot; &quot; + objTriangle2.getPointA().getY());\r\n\tSystem.out.println(&quot;Point B :&quot; + objTriangle2.getPointB().getX() +&quot; &quot; + objTriangle2.getPointB().getY());\r\n\tSystem.out.println(&quot;Point C :&quot; + objTriangle2.getPointC().getX() +&quot; &quot; + objTriangle2.getPointC().getY());\t\t\r\n\t}\r\n}\r\n<\/pre>\n<p>Incase the value of the bean wont be referred else where you can define the bean property simple as below<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans&gt;\r\n\t&lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot;&gt;\r\n\t\t&lt;property name=&quot;pointA&quot;&gt;\r\n\t\t\t&lt;bean id=&quot;point1&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t\t\t&lt;property name=&quot;x&quot; value=&quot;-20&quot;\/&gt;\r\n\t\t\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t\t\t&lt;\/bean&gt;\t\t\t\r\n\t\t&lt;\/property&gt;\r\n\t\t&lt;property name=&quot;pointB&quot; ref=&quot;point2&quot;\/&gt;\r\n\t\t&lt;property name=&quot;pointC&quot; ref=&quot;point3&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n<\/pre>\n<p>instead of <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans&gt;\r\n\t&lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot;&gt;\r\n\t\t&lt;property name=&quot;pointA&quot; ref=&quot;point1&quot;\/&gt;\r\n\t\t&lt;property name=&quot;pointB&quot; ref=&quot;point2&quot;\/&gt;\r\n\t\t&lt;property name=&quot;pointC&quot; ref=&quot;point3&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t\r\n\t&lt;bean id=&quot;point1&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;-20&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n.\r\n.\r\n.\r\n\r\n<\/pre>\n<p><strong>Using Alias<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot; name=&quot;triangleName&quot;&gt;\r\n.\r\n.\r\n.\r\n&lt;\/bean&gt;\r\n&lt;alias name=&quot;triangleId&quot; alias=&quot;triangle-alias&quot;\/&gt;\r\n<\/pre>\n<p>In Java we can refer either by name or by alias as below<br \/>\n<strong>Using Alias<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nTriangle objTriangle2 =  (Triangle)objBeanFactory.getBean(&quot;triangleName&quot;);\r\n(or)\r\nTriangle objTriangle2 =  (Triangle)objBeanFactory.getBean(&quot;triangle-alias&quot;);\r\n.\r\n.\r\n<\/pre>\n<p><strong>Using List<\/strong><br \/>\n<strong>Triangle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Triangle \r\n{\r\n    private List&lt;Point&gt; points;\t\r\n\t\r\n    public List&lt;Point&gt; getPoints() \r\n    {\r\n\treturn points;\r\n    }\r\n\r\n    public void setPoints(List&lt;Point&gt; points) \r\n    {\r\n \tthis.points = points;\r\n    }\t\t\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&gt;\r\n\t&lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot; name=&quot;triangleName&quot;&gt;\r\n\t\t&lt;property name=&quot;points&quot;&gt;\r\n\t\t\t&lt;list&gt;\r\n\t\t\t\t&lt;ref bean=&quot;point1&quot;\/&gt;\r\n\t\t\t\t&lt;ref bean=&quot;point2&quot;\/&gt;\r\n\t\t\t\t&lt;ref bean=&quot;point3&quot;\/&gt;\r\n\t\t\t&lt;\/list&gt;\t\t\r\n\t\t&lt;\/property&gt;\t\t\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;point1&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t\t&lt;property name=&quot;x&quot; value=&quot;-20&quot;\/&gt;\r\n\t\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;point2&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;0&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;bean id=&quot;point3&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;property name=&quot;x&quot; value=&quot;20&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;0&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\nList&lt;Point&gt; arrPoints = objTriangle2.getPoints();\r\n\t\t\r\n for (Point objPoint : arrPoints) \r\n {\r\n   System.out.println(&quot;Point :&quot; + objPoint.getX() +&quot; &quot; + objPoint.getY());\r\n }\r\n<\/pre>\n<p><strong>Autowiring<\/strong><br \/>\nAutowiring can be done based on name as below, byType and byConstructor.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot; name=&quot;triangleName&quot; autowire=&quot;byName&quot;&gt;\t\r\n&lt;\/bean&gt;\r\n&lt;bean id=&quot;pointA&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n  &lt;property name=&quot;x&quot; value=&quot;-20&quot;\/&gt;\r\n  &lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n&lt;\/bean&gt;\r\n&lt;bean id=&quot;pointB&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n  &lt;property name=&quot;x&quot; value=&quot;0&quot;\/&gt;\r\n  &lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n&lt;\/bean&gt;\r\n&lt;bean id=&quot;pointC&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n  &lt;property name=&quot;x&quot; value=&quot;20&quot;\/&gt;\r\n  &lt;property name=&quot;y&quot; value=&quot;0&quot;\/&gt;\r\n&lt;\/bean&gt;\r\n<\/pre>\n<p>The Name of the instanceVariable in class should match the autowired xml bean Name.<br \/>\n<strong>Triangle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\npublic class Triangle {\t\r\n\tprivate Point pointA;\r\n\tprivate Point pointB;\r\n\tprivate Point pointC;\r\n.        \r\n.\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>spring.xml &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;!DOCTYPE beans PUBLIC &quot;-\/\/SPRING\/\/DTD BEAN\/\/EN&quot; &quot;http:\/\/www.springframework.org\/dtd\/spring-beans.dtd&quot;&gt; &lt;beans&gt; &lt;bean id=&quot;triangleId&quot; class=&quot;com.mugil.shapes.Triangle&quot;\/&gt; &lt;\/beans&gt; Triangle.java package com.mugil.shapes; public class Triangle { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } public void drawShape() { System.out.println(&quot;Shape of Triangle&quot;); } } Reading value of Bean by&hellip; <a href=\"https:\/\/codethataint.com\/blog\/1506\/\">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-1506","post","type-post","status-publish","format-standard","hentry","category-form-elements-spring"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1506","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=1506"}],"version-history":[{"count":8,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1506\/revisions"}],"predecessor-version":[{"id":1514,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1506\/revisions\/1514"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}