{"id":4697,"date":"2017-03-20T20:36:43","date_gmt":"2017-03-20T20:36:43","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4697"},"modified":"2017-03-20T20:36:43","modified_gmt":"2017-03-20T20:36:43","slug":"using-spring-annotations","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-spring-annotations\/","title":{"rendered":"Using spring annotations"},"content":{"rendered":"<p>In a previous post I implemented a simple bean and created the SpringBeans configuration file, but spring can also be used with annotations (attributes to us .NET people).<\/p>\n<p>So let&#8217;s take the previous posts code and amend it to use annotations.<\/p>\n<ul>\n<li>Remove or comment out the contents of SpringBeans.xml<\/li>\n<li>Change the HelloWorld bean to look like this\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class HelloWorld {\r\n    private String name;\r\n\r\n    public void setName(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public void output() {\r\n        System.out.println(&quot;Bean says &quot; + name);\r\n    }\r\n}\r\n<\/pre>\n<\/li>\n<li>Change the main method to look like this\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\r\n\r\npublic static void main( String&#x5B;] args )\r\n{\r\n   AnnotationConfigApplicationContext context =\r\n      new AnnotationConfigApplicationContext(IocConfiguration.class);\r\n\r\n   HelloWorld obj = (HelloWorld) context.getBean(&quot;helloBean&quot;);\r\n   obj.output();\r\n}\r\n<\/pre>\n<\/li>\n<li>We still need a way to define how our beans are going to be created. We&#8217;re going to do this via a class, which we&#8217;ll name IocConfiguration (the name is unimportant), here&#8217;s the class\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\npublic class IocConfiguration {\r\n\r\n    @Bean(name = &quot;helloBean&quot;)\r\n    public HelloWorld getHelloWorld() {\r\n        HelloWorld o = new HelloWorld();\r\n        o.setName(&quot;Hello World&quot;);\r\n        return o;\r\n    }\r\n}\r\n<\/pre>\n<\/li>\n<li>Now before you can using the @Configuration you&#8217;ll need to update the Maven pom.xml to download cglib, so add the following dependency\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;cglib&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;cglib&lt;\/artifactId&gt;\r\n      &lt;version&gt;2.2.2&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n<\/pre>\n<\/li>\n<li>Run mvn install and now build and run the application.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post I implemented a simple bean and created the SpringBeans configuration file, but spring can also be used with annotations (attributes to us .NET people). So let&#8217;s take the previous posts code and amend it to use annotations. Remove or comment out the contents of SpringBeans.xml Change the HelloWorld bean to look [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161,162],"tags":[],"class_list":["post-4697","post","type-post","status-publish","format-standard","hentry","category-java","category-spring"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4697"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4697\/revisions"}],"predecessor-version":[{"id":4702,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4697\/revisions\/4702"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}