Creating a simple spring bean

I’m going to create the standard Hello World spring bean. To start with, let’s use Maven to create a simple “default” set of files and from this we’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 add the following

<properties>
   <spring.version>3.0.5.RELEASE</spring.version>
</properties>

and within the dependencies element add the following

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
</dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
</dependency>

Back to the command line, run (from the folder where your pom.xml is located)

mvn install

This command will run the pom.xml and install the spring framework dependencies.

Let’s add a bean

Beans are simply classes which are managed by the spring framework, so let’s create the obligatory HelloWorld bean

package com.putridparrot.core;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void output() {
        System.out.println("Bean says " + name);
    }
}

Now we’ll need to change the main method generated by Maven to look like this

ApplicationContext context = 
   new ClassPathXmlApplicationContext(
      "SpringBeans.xml");

HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.output();   

You’ll also need the following imports

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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.

You’ll notice that we’re telling spring to use the SpringBeans.xml file, which we haven’t yet created. In your main folder create a resources folder and within this add create the file SpringBeans.xml. It should look like this

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="helloBean" class="com.putridparrot.core.HelloWorld">
        <property name="name" value="Hello World" />
    </bean>
</beans>

So in the above we create the identifier helloBean, as stated previously, when we ask for an object with this identifier spring will supply a com.putridparrot.core.HelloWorld instance. We also supply a property value which is injected to the setName method (Java doesn’t support properties like C# so name is translated to setName). Thus, the value Hello Worldoutput method to see whether it all worked.