January 11, 2010

Spring Framework Example

« Java Servlet API - Difference between forward, include, redirect | Main | Java Scripting Example »

The Spring Framework represents a dependency injection / inversion of control container framework for the Java platform. Furthermore Spring offers a lot of additional functions. For example it integrates Aspect Oriented Programming, Transaction handling, Data Access functionality, implementation for remote accessing and so on. The following intuitive example describes the basic usage.

Use case:

  • Customer-class: Basic class, which contains customer data
  • DataSource-class: In-memory datasource, that write and read the customers
  • CustomerManager-class: Lists all customers on System.out.
Customer Java class:
package org.developers.blog.spring.example;

/**
 *
 * @author Rafael Sobek
 */
public class Customer {
    private String name;

    public String getName() {
        return name;
    }

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

    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return id + "|" + name + "|" + address;
    }
}
DataSource Java class:
package org.developers.blog.spring.example;

import java.util.Collection;
import java.util.Map;

/**
 *
 * @author Rafael Sobek
 */
public class DataSource {

    public Map getData() {
        return data;
    }

    public void setData(Map data) {
        this.data = data;
    }

    private Map data;

    int i = 0;

    public void addCustomer(Customer customer) {
        customer.setId(i++);
        data.put(customer.getId(), customer);
    }

    public Collection getCustomers() {
        return data.values();
    }

}
CustomerManager Java class:
package org.developers.blog.spring.example;

/**
 *
 * @author Rafael Sobek
 */
public class CustomerManager {
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void listCustomersOnConsole() {
        for (Customer customer:this.dataSource.getCustomers()) {
            System.out.println("Customer: " + customer);
        }
    }
}
Springframework instrumentalization code:
<?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.xsd">

  <!-- Initialization of DataSource object. Add some customers to data member -->
  <bean id="dataSource" class="org.developers.blog.spring.example.DataSource">
        <property name="data">
            <map>
                <entry key="0">
                    <bean class="org.developers.blog.spring.example.Customer">
                        <property name="id" value="0"/>
                        <property name="name" value="Rafael Sobek"/>
                        <property name="address" value="Nirvana 1"/>
                    </bean>
                </entry>
                <entry key="1">
                    <bean class="org.developers.blog.spring.example.Customer">
                        <property name="id" value="0"/>
                        <property name="name" value="Nobody"/>
                        <property name="address" value="Nirvana 2"/>
                    </bean>
                </entry>
            </map>
        </property>
  </bean>

  <!-- references DataSource object and lists the customer -->
  <bean id="customerManager" class="org.developers.blog.spring.example.CustomerManager">
        <property name="dataSource" ref="dataSource"/>
  </bean>

</beans>
SpringExampleTest class for execution of Springframework instrumentalization:
package org.developers.blog.spring.example;

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

public class SpringExampleTest {

    @Test
    public void testInstrumentalisation() {
        ApplicationContext appCtx =
                new ClassPathXmlApplicationContext("application-ctx.xml");
        CustomerManager customerManager =
                (CustomerManager) appCtx.getBean("customerManager");
        customerManager.listCustomersOnConsole();
    }
}
You can download the complete example as Maven project here. To run test instrumentalization you have to execute mvn test.

Have fun
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 3:01 PM in Java

 

[Trackback URL for this entry]

Pingback: Spring Framework Example | Programming News Station at Mi, 13 Jan 9:11 AM

Spring Framework Example
Blog - Programming Languages , Technologies and Visions. Topics: Visions, Architecture Issues, Programming Languages , .. Read more here: Spring Framework

Comment: Evans at Mo, 10 Mai 2:06 PM

Nice article, but it would be nice if you added a good discussion of what Spring Framework is and why anyone might want to use it.

Cracking post BTW :)

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« January »
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
24252627282930
31