Wednesday, June 10, 2009

Spring 3 Simplest App


Creating a simple application in Spring 3 (I am using spring-framework-3.0.0.M3)


The artifacts are: 

  • Spring configuration file
  • Main Application
  • Single POJO bean to be wired
  • Library dependencies
  • Maven project (not strictly needed, but may be useful if you use Maven)


Spring configuration file: simple.xml on classpath


<?xml version="1.0" encoding="UTF-8"?>

<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="simplePersonPojo" class="com.jgk.spring3.simple.SimplePersonPojo">

  </bean>


</beans>


Main Application


package com.jgk.spring3.simple;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class CheckSpringApp {


public static void main(String[] args) { 

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "simple.xml",});

                SimplePersonPojo bean = (SimplePersonPojo) context.getBean("simplePersonPojo");

// ... do some things with the bean here ...

}


}



Single POJO bean to be wired


package com.jgk.spring3.simple;


public class SimplePersonPojo {

private String title;

private String address;

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}


public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}



Library dependencies

  • antlr-3.0.1
  • commons-logging-1.1.1
  • org.springframework.asm-3.0.0.M3
  • org.springframework.beans-3.0.0.M3
  • org.springframework.core-3.0.0.M3
  • org.springframework.context-3.0.0.M3
  • org.springframework.expression-3.0.0.M3


Needed to manually add the libraries to local Maven repository

Maven pom.xml:

<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.jgk.spring3</groupId>

  <artifactId>jgkspring3</artifactId>

  <packaging>jar</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <properties>

    <spring.version>3.0.0.M3</spring.version>

  </properties>

  <name>Playing with Spring Framework 3</name>

  <url>http://maven.apache.org</url> 

  <dependencies>

    <dependency>

      <groupId>commons-logging</groupId>

      <artifactId>commons-logging</artifactId>

      <version>1.1.1</version>

    </dependency>

    <dependency>

      <groupId>org.antlr</groupId>

      <artifactId>antlr</artifactId>

      <version>3.0.1</version>

      <scope>compile</scope>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>org.springframework.asm</artifactId>

      <version>${spring.version}</version>

    </dependency> 

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>org.springframework.beans</artifactId>

      <version>${spring.version}</version>

    </dependency>

     <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>org.springframework.core</artifactId>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>org.springframework.context</artifactId>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>org.springframework.expression</artifactId>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupId>log4j</groupId>

      <artifactId>log4j</artifactId>

      <version>1.2.14</version>

      <optional>true</optional> 

    </dependency>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  

  <repositories>

    <repository>

      <id>axle</id>

      <name>axle</name>

      <url>http://axle/gsmaven2/repository</url>

    </repository>

  </repositories>

</project>


4 comments:

Kamil said...

Thanks a lot for this tutorial ;)

javapda blog said...

You're welcome

Ravi Mukkavilli said...

Sir, Thank you for the nice tutorial. I'm just beginning Spring 3.0, and planning to use Maven2. But I'm not able to find a Spring 3.0 artifact in the maven repo, even Spring 2.5 doesn't exist there. It would be great if you can guide me through. Also can you post article explaining the jar files that comes with Spring 3, as the names have totally changed. Thank you.
-Ravi

javapda blog said...

Ravi, have a look at http://blog.springsource.com/2007/09/18/maven-artifacts-2/ it lists the repositories.