注意这个工程中有3个source folder:src,resources,test。在Eclipse中,它们的图标与普通文件夹是不同的。实际上,这3个文件夹相当于同一个文件夹。
application-ctx.xml
<?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="1"/>
<property name="name" value="宅未央"/>
<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>
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();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.developers.blog.spring.example;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
/**
*
* @author rafsob
*/
public class DataSource {
public Map<Integer, Customer> getData() {
return data;
}
public void setData(Map<Integer, Customer> data) {
this.data = data;
}
private Map<Integer, Customer> data;
int i = 0;
public void addCustomer(Customer customer) {
customer.setId(i++);
data.put(customer.getId(), customer);
}
public Collection<Customer> getCustomers() {
return data.values();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.developers.blog.spring.example;
/**
*
* @author rafsob
*/
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);
}
}
}
给Eclipse工程中添加Junit库:
运行程序:
源代码,原文:
http://pan.baidu.com/share/link?shareid=423718&uk=3878681452
http://developers-blog.org/blog/d/Java/2010/01/11/Spring-Framework-Example