{"id":4694,"date":"2017-03-20T20:32:42","date_gmt":"2017-03-20T20:32:42","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4694"},"modified":"2017-03-20T20:32:42","modified_gmt":"2017-03-20T20:32:42","slug":"creating-a-simple-spring-bean","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-simple-spring-bean\/","title":{"rendered":"Creating a simple spring bean"},"content":{"rendered":"<p>I&#8217;m going to create the standard Hello World spring bean. To start with, let&#8217;s use Maven to create a simple &#8220;default&#8221; set of files and from this we&#8217;ll start coding our bean.<\/p>\n<p>To start with, run the following Maven command<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nmvn archetype:generate -DgroupId=com.putridparrot.core -DartifactId=SpringStarter -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false\r\n<\/pre>\n<p>Now open the pom.xml and before the dependencies element add the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;properties&gt;\r\n   &lt;spring.version&gt;3.0.5.RELEASE&lt;\/spring.version&gt;\r\n&lt;\/properties&gt;\r\n<\/pre>\n<p>and within the dependencies element add the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependency&gt;\r\n   &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;spring-core&lt;\/artifactId&gt;\r\n   &lt;version&gt;${spring.version}&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n\r\n&lt;dependency&gt;\r\n   &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\r\n   &lt;version&gt;${spring.version}&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p>Back to the command line, run (from the folder where your pom.xml is located)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nmvn install\r\n<\/pre>\n<p>This command will run the pom.xml and install the spring framework dependencies.<\/p>\n<p><strong>Let&#8217;s add a bean<\/strong><\/p>\n<p>Beans are simply classes which are managed by the spring framework, so let&#8217;s create the  obligatory HelloWorld bean<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot.core;\r\n\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<p>Now we&#8217;ll need to change the <em>main<\/em> method generated by Maven to look like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nApplicationContext context = \r\n   new ClassPathXmlApplicationContext(\r\n      &quot;SpringBeans.xml&quot;);\r\n\r\nHelloWorld obj = (HelloWorld) context.getBean(&quot;helloBean&quot;);\r\nobj.output();   \r\n<\/pre>\n<p>You&#8217;ll also need the following imports<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n<\/pre>\n<p>Spring is an IoC container and it will use (in this instance) a configuration file to define the objects (beans) to be created when an identifier (helloBean) is used.<\/p>\n<p>You&#8217;ll notice that we&#8217;re telling spring to use the SpringBeans.xml file, which we haven&#8217;t yet created. In your main folder create a <em>resources<\/em> folder and within this add create the file SpringBeans.xml. It should look like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n\thttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd&quot;&gt;\r\n\r\n    &lt;bean id=&quot;helloBean&quot; class=&quot;com.putridparrot.core.HelloWorld&quot;&gt;\r\n        &lt;property name=&quot;name&quot; value=&quot;Hello World&quot; \/&gt;\r\n    &lt;\/bean&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>So in the above we create the identifier <em>helloBean<\/em>, as stated previously, when we ask for an object with this identifier spring will supply a <em>com.putridparrot.core.HelloWorld<\/em> instance. We also supply a property value which is injected to the setName method (Java doesn&#8217;t support properties like C# so <em>name<\/em> is translated to setName). Thus, the value <em>Hello World<\/em is injected into the setName method and our main method simply calls the <em>output<\/em> method to see whether it all worked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m going to create the standard Hello World spring bean. To start with, let&#8217;s use Maven to create a simple &#8220;default&#8221; set of files and from this we&#8217;ll start coding our bean. To start with, run the following Maven command mvn archetype:generate -DgroupId=com.putridparrot.core -DartifactId=SpringStarter -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false Now open the pom.xml and before the dependencies element [&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-4694","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\/4694","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=4694"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4694\/revisions"}],"predecessor-version":[{"id":4701,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4694\/revisions\/4701"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}