Spring 2.5 example

  • Prerequisites:
    • Spring 2.5.6
    • Maven 3.0.3
    • IntelliJ Idea or any IDE of your preference
    • JDK 1.6
  • Maven generate template: Run the following command, on terminal,

$ mvn archetype:generate -DgroupId=com.naveenlabs.core -DartifactId=Spring-2.5-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will generate a template of Java project to get started, with all the standard folder structure for you, except for few folders such as resources.

  • Import the project to your preferred IDE or even a decent text editor.
  • Edit the pom.xml inside the generated Spring-2.5-example, and add Spring dependency,
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
	http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.naveenlabs.core</groupId>
	<artifactId>Spring-2.5-example</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>Spring-2.5-example</name>
	<url>http://maven.apache.org</url>
	<dependencies>

		<!-- Spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
		</dependency>

	</dependencies>
</project>
  • Now, update dependencies, you can choose to run either,

$ mvn dependency:resolve

or,

$ mvn install

or even,

$ mvn install

  • Create Spring bean class inside src/main/java/com.naveenlabs.core/SpringDemo.java, and add this content,

package com.naveenlabs.core;

/**
* Spring bean
*
*/
public class SpringDemo {
private int one;
private int two;

public void setOne(int one) {
this.one = one;
}

public void setTwo(int two) {
this.two = two;
}

public void printSum() {

System.out.println(“Sum is -> ” + (one+two));
}
}

So, you have created a simple Spring bean, which takes two parameters, one and two, and show their sum. How will you pass these values? Wait for the Spring magic in further steps to come!

  • Create a new folder inside src/main called resources and add a file of your name choice, I picked spring-context.xml, and put this content inside it,

<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-2.5.xsd”>

<bean id=”springDemoBean” class=”com.naveenlabs.core.SpringDemo”>
<property name=”one” value=”1″/>
<property name=”two” value=”2″/>
</bean>

</beans>

This is a context configuration file, here you are declare all the available Spring beans. You can have multiple context files, depending on your requirement. The above configuration is self-explanatory. You define a bean by id and give it a name of your choice, I gave springDemoBean and you need to give the class for that bean too. Then the setter methods will get the parameters. We have two attributes, one and two, and we supply the desired value to them. Do not worry about passing the value inside double quotes, as Spring knows what is the data type of the attribute and will give the exact to the bean.

  • Now, in App.java which is a default main class generated by Maven, you can chose to create your own too, add this content,
package com.naveenlabs.core;

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

/**
 * Spring Demo!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext(
            "spring-context.xml");

      SpringDemo obj = (SpringDemo) context.getBean("springDemoBean");
      obj.printSum();
    }
}

You need to load the context file to load the context, and then get the desired bean, in our case springDemoBean. Now, you can call the printSum() method to print the sum of values provided.

  • Run this command to build the project,

$ mvn clean install

It should build the project with dependencies and create a folder target in the root of the project folder.

  • Now, to run the App.java as it is our main class, you can either do it with IDE if you have imported the project or through command line by running the command:

$  java -cp target/Spring-core-2.5.jar com.naveenlabs.core.App

output of the above command will be,

$ Sum is -> 3

If you encounter an error something like,

$  java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

then you could execute the program through the command,

mvn exec:java -Dexec.mainClass=com.naveenlabs.core.App

and that should give you the output.