Spring 3.0 example

This tutorial will try to create the Spring core application as mentioned in Spring 2.5 example, but using Spring 3.0 version.

The application is going to be a bit different from Spring 2.5 example.

  • Prerequisites:
    • Spring 3.0.5.RELEASE
    • Maven 3.1.1
    • JDK 1.7
    • IntelliJ IDEA
  • Generate template using maven: 

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

  • Import the project to your favourite text editor or IDE, I have used IntelliJ
  • Edit your POM file and add the dependencies:

<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-3-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring-3-example</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!– Spring 3 dependencies –>
<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>
</dependencies>
</project>

  • Now, either do,

$ mvn clean install

or,

$ mvn dependency:resolve

to download the dependencies.

  • Now, create three classes inside, /src/main/java/com/naveenlabs/core, named, SpringBeanA.java, SpringBeanB.java and SpringBeanC.java. We will have a String attribute each in SpringBeanA and SpringBeanB class. The idea is to concatenate both the Strings of those classes and assign it to SpringBeanC class attribute to print the concatenated String.

Here is SpringBeanA.java,

package com.naveenlabs.core;

/**
* Created by naveen.
*/
public class SpringBeanA {
private String stringA;

public String getStringA() {
return stringA;
}

public void setStringA(String stringA) {
this.stringA = stringA;
}
}

Now, SpringBeanB.java,

package com.naveenlabs.core;

/**
* Created by naveen.
*/
public class SpringBeanB {

private String stringB;

public String getStringB() {
return stringB;
}

public void setStringB(String stringB) {
this.stringB = stringB;
}
}

And, now create SpringBeanC.java,

package com.naveenlabs.core;

/**
* Created by naveen.
*/
public class SpringBeanC {

private String resultString;

public void setResultString(String resultString) {
this.resultString = resultString;
}

public void printString(){
System.out.println(“Result string -> “+resultString);
}
}

  • now, create resources folder inside /src/main/ and add spring-3-core-context.xml file. Once the configuration file is created add these contents 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-3.0.xsd”>

<bean id=”springBeanA” class=”com.naveenlabs.core.SpringBeanA”>
<property name=”stringA” value=”Hello” />
</bean>

<bean id=”springBeanB” class=”com.naveenlabs.core.SpringBeanB”>
<property name=”stringB” value=” World Naveen Labs!” />
</bean>

<bean id=”springBeanC” class=”com.naveenlabs.core.SpringBeanC”>
<property name=”resultString” value=”#{springBeanA.getStringA()+springBeanB.getStringB()}” />
</bean>

</beans>

As you can see, defining SpringbeanA and SpringBeanB is is self-explanatory, but the definition of SpringBeanC  is slightly different. In SpringBeanC definition, we are assigning and concatenating the return value of SpringBeanA and SpringBeanB getter methods, i.e; #{springBeanA.getStringA()+springBeanB.getStringB()}, to the attribute of SpringBeanC.

  • Now, in the main class, i.e; App.java in our case, add the following,
package com.naveenlabs.core;

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

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

        SpringBeanC obj = (SpringBeanC) context.getBean("springBeanC");
        obj.printString();
    }
}
  • Build the application,

$ mvn clean install

  •  Run the App.java,

java -cp target/Spring-3-example-1.0-SNAPSHOT.jar com.naveenlabs.core.App

  • The output will be,

Result string -> Hello World Naveen Labs!