story-configuration.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="StoryList" class="StoryList">
<property name="stories">
<list>
<ref bean="HelloWorld" />
<ref bean="OnceUpon" />
</list>
</property>
</bean>
<bean id="HelloWorld" class="Story">
<property name="title">
<value>Hello World!</value>
</property>
<property name="author">
<value>User:Hyad</value>
</property>
<property name="content">
<value>This is a simple story.
This is a story about a man who said, "Hello, World."
See, now wasn't that simple.</value>
</property>
</bean>
<bean id="OnceUpon" class="Story">
<property name="title">
<value>Once Upon a Time</value>
</property>
<property name="author">
<value>Rob Yates</value>
</property>
<property name="content">
<value>Once upon a time there were some interesting things going on.
The end.</value>
</property>
</bean>
</beans>
import java.util.List;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class StoryReader {
public static void main(String args[]) {
XmlBeanFactory beanFactory = new XmlBeanFactory(
new ClassPathResource("story_configuration.xml"));
StoryList storyList = (StoryList) beanFactory.getBean("StoryList");
List myStories = storyList.getStories();
for (int i = 0; i < myStories.size(); i++) {
Story currentStory = (Story) myStories.get(i);
System.out.println("\"" + currentStory.getTitle() + "\" by "
+ currentStory.getAuthor() + ":");
System.out.println(currentStory.getContent());
System.out.println();
}
}//end main
}//end StoryReader
import java.util.List;
public class StoryList {
private List stories;
public StoryList (){
}
public void setStories(List stories) {
this.stories = stories;
}
public List getStories() {
return stories;
}
}
原文:https://en.wikibooks.org/wiki/Java_Programming/Spring_framework
源代码:http://pan.baidu.com/share/link?shareid=421959&uk=3878681452
有关依赖注入的一点解释:http://188029.net/spring/spring2.html