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.
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: Java Spring Springframework Spring Example

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