{"id":1528,"date":"2016-09-01T14:17:15","date_gmt":"2016-09-01T14:17:15","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1528"},"modified":"2016-09-01T17:18:49","modified_gmt":"2016-09-01T17:18:49","slug":"spring-annotations1","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-annotations1\/","title":{"rendered":"Spring Annotations1"},"content":{"rendered":"<p><strong>@Required<\/strong><br \/>\nIncase the bean value is not defined using required will display the spring error message instead of null pointer exception for the bean whose value is not defined.<\/p>\n<p><strong>spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;\t\t\r\n&lt;\/bean&gt;\r\n<\/pre>\n<p><em>Note above the pointA is undefined for above circle bean<\/em><\/p>\n<p><strong>Spring Error without @Required annotation<\/strong><\/p>\n<pre>\r\nException in thread \"main\" java.lang.NullPointerException\r\n\tat com.mugil.shapes.Circle.drawShape(Circle.java:11)\r\n\tat com.mugil.shapes.DrawingApp.main(DrawingApp.java:17)\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\t@Required\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>spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;\t\t\r\n&lt;\/bean&gt;\r\n\t\r\n&lt;bean class=&quot;org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor&quot;\/&gt;\r\n<\/pre>\n<p><strong>Spring Error is Thrown<\/strong><\/p>\n<pre>\r\nException in thread \"main\" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleId' defined in class path resource [spring1.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'center' is required for bean 'circleId'\r\n<\/pre>\n<p><strong>Autowired Annotation<\/strong><\/p>\n<ol>\n<li>@Autowired works by taking into consideration type,name,qualifier<\/li>\n<li>If type not found name will be considered<\/li>\n<\/ol>\n<p><strong>spring.xml(by Type)<\/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;0&quot;\/&gt;\r\n\t\t&lt;property name=&quot;y&quot; value=&quot;20&quot;\/&gt;\r\n\t&lt;\/bean&gt;\t\r\n\t&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;&lt;\/bean&gt;\t\r\n\t&lt;bean class=&quot;org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><strong>spring.xml(by Name)<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans&gt;\r\n\t&lt;bean id=&quot;center&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;20&quot;\/&gt;\r\n\t&lt;\/bean&gt;\t\r\n\t&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;&lt;\/bean&gt;\t\r\n\t&lt;bean class=&quot;org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor&quot;\/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>In above xml center is the name of instance variable in circle class<br \/>\n<strong>spring.xml(by Qualifier)<\/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;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans \r\n\t    http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n\t\thttp:\/\/www.springframework.org\/schema\/context \r\n\t\thttp:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt;\t\t\r\n\t&lt;bean id=&quot;pointA&quot; class=&quot;com.mugil.shapes.Point&quot;&gt;\r\n\t\t&lt;qualifier value=&quot;circle&quot;\/&gt;\t\t\t\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;\t\r\n\t&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;&lt;\/bean&gt;\t\r\n\t&lt;bean class=&quot;org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor&quot;\/&gt;\r\n&lt;\/beans&gt;\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\t@Autowired\r\n\t@Qualifier(&quot;circle&quot;)\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\t\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","protected":false},"excerpt":{"rendered":"<p>@Required Incase the bean value is not defined using required will display the spring error message instead of null pointer exception for the bean whose value is not defined. spring.xml &lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt; &lt;\/bean&gt; Note above the pointA is undefined for above circle bean Spring Error without @Required annotation Exception in thread &#8220;main&#8221; java.lang.NullPointerException at&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-annotations1\/\">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-1528","post","type-post","status-publish","format-standard","hentry","category-form-elements-spring"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1528","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=1528"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1528\/revisions"}],"predecessor-version":[{"id":1531,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1528\/revisions\/1531"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}