spring 基础1

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值