{"id":1533,"date":"2016-09-02T05:18:07","date_gmt":"2016-09-02T05:18:07","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1533"},"modified":"2016-09-02T16:22:30","modified_gmt":"2016-09-02T16:22:30","slug":"spring-annotations-2","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-annotations-2\/","title":{"rendered":"Spring Annotations 2"},"content":{"rendered":"<p><strong>Java Specification Request JSR 250<\/strong><\/p>\n<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;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\r\n\t&lt;bean id=&quot;pointC&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; \t\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;\t\r\n\t&lt;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt;\r\n\t&lt;\/bean&gt;\t\r\n\t&lt;context:annotation-config\/&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{\t\t\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@Resource(name=&quot;pointC&quot;)\r\n\tpublic void setCenter(Point center) {\r\n\t\tthis.center = center;\r\n\t}\r\n\t\r\n\t@PostConstruct\r\n\tpublic void initilizeMe() \r\n\t{\r\n\t\tSystem.out.println(&quot;Initialized&quot;);\r\n\t}\r\n\t\r\n\t@PreDestroy\r\n\tpublic void destroyMe() \r\n\t{\r\n\t\tSystem.out.println(&quot;Derstroyed&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p><em>If name is not specified after the @Resource it will look for same name matching bean in the spring.xml file.<\/em><\/p>\n<p>@PostConstruct and @PreDestroy will be called when circle bean is initialized and destroyed<\/p>\n<p><strong>Defining Bean using Annotations<\/strong><br \/>\n<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;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;\r\n&lt;context:component-scan base-package=&quot;com.mugil.shapes&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\n@Component\r\npublic class Circle implements Shape\r\n{\t\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><strong>drawingApp.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 AbstractApplicationContext objContext = new ClassPathXmlApplicationContext(&quot;spring1.xml&quot;);\r\n\t\t objContext.registerShutdownHook();\r\n\t\tShape objShape =  (Shape)objContext.getBean(&quot;circle&quot;);\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><strong>Using MessageSource<\/strong><br \/>\n<strong>spring.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean id=&quot;messageSource&quot; class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;&gt;\r\n\t\t&lt;property name=&quot;basenames&quot;&gt;\r\n\t\t\t&lt;list&gt;\r\n\t\t\t\t&lt;value&gt;constant&lt;\/value&gt;\r\n\t\t\t&lt;\/list&gt;\r\n\t\t&lt;\/property&gt;\r\n\t&lt;\/bean&gt;\r\n<\/pre>\n<p><strong>constant.properties<\/strong><\/p>\n<pre>\r\nTestMessage=Mugil\r\n<\/pre>\n<p><strong>DrawingApp.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n AbstractApplicationContext objContext = new ClassPathXmlApplicationContext(&quot;spring1.xml&quot;);\r\nSystem.out.println(objContext.getMessage(&quot;TestMessage&quot;, null, &quot;Default Message&quot;, null));\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n Mugil\r\n<\/pre>\n<p><strong>Using instance of Message Source in Bean Class<\/strong><br \/>\n<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;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;\r\n&lt;bean id=&quot;messageSource&quot; class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;&gt;\r\n\t\t&lt;property name=&quot;basenames&quot;&gt;\r\n\t\t\t&lt;list&gt;\r\n\t\t\t\t&lt;value&gt;constant&lt;\/value&gt;\r\n\t\t\t&lt;\/list&gt;\r\n\t\t&lt;\/property&gt;\r\n\t&lt;\/bean&gt;\r\n\t&lt;context:annotation-config\/&gt;\r\n\t&lt;context:component-scan base-package=&quot;com.mugil.shapes&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\n@Component\r\npublic class Circle implements Shape{\t\t\r\n\tprivate Point center;\r\n\t\r\n\t@Autowired\r\n\tprivate MessageSource messageSource;\t\r\n\t\r\n\t@Override\r\n\tpublic void drawShape() {\r\n\t\tSystem.out.println(&quot;Shape of Circle &quot;);\t\t\r\n\t\tSystem.out.println(this.messageSource.getMessage(&quot;TestMessage&quot;, null, &quot;Vannan&quot;, null));\r\n\t}\r\n\r\n\tpublic Point getCenter() {\r\n\t\treturn center;\r\n\t}\r\n\r\n\t@Resource(name=&quot;pointC&quot;)\r\n\tpublic void setCenter(Point center) {\r\n\t\tthis.center = center;\r\n\t}\r\n\t\r\n\tpublic MessageSource getMessageSource() {\r\n\t\treturn messageSource;\r\n\t}\r\n\r\n\tpublic void setMessageSource(MessageSource messageSource) {\r\n\t\tthis.messageSource = messageSource;\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<p><strong>this.messageSource.getMessage(&#8220;TestMessage&#8221;, null, &#8220;Vannan&#8221;, null)<\/strong><\/p>\n<p><strong>Variable substitution in Message Source<\/strong><\/p>\n<p><strong>circle.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Component\r\npublic class Circle implements Shape{\t\t\r\n\tprivate Point center;\r\n\t\r\n\t@Autowired\r\n\tprivate MessageSource messageSource;\r\n\t\r\n\t\r\n\t@Override\r\n\tpublic void drawShape() {\r\n\t\tSystem.out.println(&quot;Center of Cirlce&quot;);\t\tSystem.out.println(this.messageSource.getMessage(&quot;TestMessage2&quot;, new Object&#x5B;]{center.getX(), center.getY()}, &quot;Vannan&quot;, null));\r\n\t}\r\n.\r\n.\r\n.\r\n}\r\n<\/pre>\n<p><strong>this.messageSource.getMessage(&#8220;TestMessage2&#8221;, new Object[]{center.getX(), center.getY()}, &#8220;Vannan&#8221;, null)<\/strong><\/p>\n<p><strong>constant.properties<\/strong><\/p>\n<pre>\r\nTestMessage=Mugil\r\nTestMessage2=Center of Circle : ({0}, {1})\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java Specification Request JSR 250 spring.xml &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot; xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd http:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context-2.5.xsd&quot;&gt; &lt;bean id=&quot;pointC&quot; class=&quot;com.mugil.shapes.Point&quot;&gt; &lt;property name=&quot;x&quot; value=&quot;20&quot;\/&gt; &lt;property name=&quot;y&quot; value=&quot;0&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;bean class=&quot;com.mugil.shapes.Circle&quot; id=&quot;circleId&quot;&gt; &lt;\/bean&gt; &lt;context:annotation-config\/&gt; &lt;\/beans&gt; circle.java public class Circle implements Shape{ private Point center; @Override public void drawShape() { System.out.println(&quot;Shape of&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-annotations-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-1533","post","type-post","status-publish","format-standard","hentry","category-form-elements-spring"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1533","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=1533"}],"version-history":[{"count":7,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1533\/revisions"}],"predecessor-version":[{"id":1536,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1533\/revisions\/1536"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}